0

我尝试从命令提示符启动一个名为“long name here.ps1”的长 powershell 脚本。但我也试图确保它在 powershell 中作为管理员命令运行。我在 powershell 中设置了所有执行策略,因此我使用了ss64 set-executionpolicy command guide for powershell来使 powershell 正常工作。但是我正在尝试使用另一个 stackoverflow 问题中的解决方案,该问题涉及以管理员身份运行命令。我正在运行一个需要以管理员身份执行 powershell 脚本 (.ps1) 的批处理脚本,我不介意用户是由 UAC 提示还是输入密码。我目前正在使用以下命令:

powershell.exe -command "&{ Start-Process powershell -ArgumentList '-noprofile -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 '-noprofile -file C:\long` name` here.ps1' -verb RunAs}"

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

此外,我完全不知道如何将参数发送到实际脚本。

4

3 回答 3

0

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

如果允许使用免费的第三方可执行文件,您可以使用我编写的名为elevate32.exe(32-bit) 和elevate64.exe(64-bit) 的简短工具以管理员身份启动 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"
于 2017-11-27T16:59:39.370 回答
0

如果我正确地阅读了您的问题 - powershell 不会找到该文件,因为它在遇到空格时会停止读取路径名?

此处给出的示例指定了;以管理员身份从命令提示符运行的 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}"

也可能值得在这里查看其他答案

于 2017-11-27T16:57:58.657 回答
-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
于 2017-11-27T17:27:56.647 回答