首页 文章

如何从Windows命令提示符(CMD)运行Long Powershell脚本

提问于
浏览
0

我尝试从命令提示符启动名为"long name here.ps1"的长PowerShell脚本 . 但我也试图确保它在powershell中作为管理员命令运行 . 我在powershell中设置了所有执行策略,因此我使用了ss64 set-executionpolicy command guide for powershell来获得PowerShell的工作 . 但我试图使用another stackoverflow question中的解决方案,该解决方案以管理员身份运行命令 . 我正在运行需要以管理员身份执行powershell脚本(.ps1)的批处理脚本,我不介意UAC是否提示用户或密码 . 我目前正在使用以下命令:

powershell.exe -command“&{Start-Process powershell -ArgumentList'-noroprofile -file”C:\ long name here.ps1“' - verb RunAs}”

我在https://ss64.com/ps/powershell.html底部找到了这个命令,其中有关于如何以管理员身份运行powershell命令的详细信息 . 该代码的问题在于我的powershell脚本1.具有参数,而2.具有长名称 . 我已经尝试过很多次此命令的迭代而没有成功,下面列出了那些不起作用的迭代:

powershell.exe -command“&{Start-Process powershell -ArgumentList'-noroprofile -file C:\ longname here.ps1'-verb RunAs}”powershell.exe -command“&{Start-Process powershell -ArgumentList '-noprofile -file:“C:\ long name here.ps1' -verb RunAs}”

另外,我完全迷失了如何将参数发送到实际脚本 .

3 回答

  • -1

    当您使用 PowerShell.exe -Command 时,您不需要使用引号 . 例如,您可以运行以下命令:

    PowerShell.exe -Command Get-Service 'wuauserv'
    

    -Command 之后的所有内容都被解释为命令 . 另请注意,CMD中的双引号需要使用反斜杠进行转义 . 因此:

    powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\"' -Verb RunAs
    

    如果您的文件有参数:

    powershell.exe -Command Start-Process PowerShell -ArgumentList '-NoProfile -File \"C:\long name here.ps1\" \"Arg1\" \"Arg2\"' -Verb RunAs
    
  • -1

    如果我正确地阅读你的问题 - powershell会找不到文件,因为当它遇到一个空格时它会停止读取路径名吗?

    给出的示例here指定;要从命令提示符运行的powershell命令,管理员应具有以下语法:

    powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file MyScript.ps1' -verb RunAs}"
    

    几种方法来实现您正在寻找的东西 . 但最简单的方法是使用 ` 字符来转义引号 . 所以类似于;

    powershell.exe -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -file `"C:\long file name.ps1`"' -verb RunAs}"
    

    也许值得查看其他答案here

  • 0

    使用免费软件第三方实用程序

    如果允许使用免费软件第三方可执行文件,您可以使用我编写的名为 elevate32.exe (32位)和 elevate64.exe (64位)的简短工具以管理员身份启动powershell.exe,并使用 -File 参数和您想要的脚本参数使用:

    elevate64 -- powershell.exe -File "<path>\<long script name>.ps1" -Arg "<long script argument>"
    

    您可以从www.westmesatech.com获取该工具(受版权保护的免费软件,可在任何地方免费使用,无需安装) .

    使用WSH脚本

    如果您可以使用Windows脚本宿主(WSH)脚本 -- 参数,那么 elevate.js

    var args = WScript.Arguments;
    if ( args.Length >= 1 ) {
        var exec = args.Item(0);
        var cmdLine = "";
        for (var i = 1; i < WScript.Arguments.Length; i++ ) {
          cmdLine += cmdLine == "" ? '"' + args.Item(i) + '"' : ' "' + args.Item(i) + '"';
        }
        var shellApp = new ActiveXObject("Shell.Application");
        shellApp.ShellExecute(exec, cmdLine, "", "runas");
    }
    

    你可以打电话如下:

    wscript.exe "d:\path\elevate.js" powershell.exe -File "C:\long path\script name.ps1" "long script argument"
    

    自我提升您的PowerShell脚本

    另一种选择是编写一个自我提升的PowerShell脚本 . 您可以检查脚本中的高程;如果没有提升,它可以自我升级并运行您需要的任何命令 . 例:

    $isElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    
    if ( -not $isElevated ) {
      Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
      exit
    }
    
    & "d:\long path name\script name.ps1" "Long Argument 1" "Long Argument 2"
    

相关问题