![](/img/trans.png)
[英]Running cmd.exe through start-process but unable to pass the command to cmd.exe
[英]Powershell: Start-Process doesn't pass arguments to cmd.exe
这些是在 powershell 控制台(Windows 10)中运行的命令:
$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process powershell.exe -Credential $credential -WindowStyle Hidden -ArgumentList "Start-Process cmd.exe -Verb RunAs -ArgumentList 'value'"
这些命令可以正常工作,除非您通过另一个用户以管理员身份打开 cmd.exe,运行以下命令:
echo %1
它给了我:
%1
字面上地。 相反,我期望:
value
我究竟做错了什么?
我刚刚回答了一个问题,可以使用我在那里提供的脚本找到解决方案,并进行一些修改,并以特定方式调用命令链:
运行代理.ps1
# First arg should be the script path
$script = $args[0]
# Rest of args should be any script parameters
$scriptArgs = $args[1..$args.Count] -join ' '
$startProcessArgs = @{
Wait = $true
Verb = 'RunAs'
FilePath = 'cmd.exe'
ArgumentList = "/c ""$script"" $scriptArgs"
}
Start-Process @startProcessArgs
exit $LASTEXITCODE
然后调用RunAsProxy.ps1作为您要运行的用户,如下所示,然后提升:
$command = "Command_you_want_to_run_from_cmd.exe"
$arguments = "any arguments to the program"
Start-Process -Cred $credential powershell.exe "-File ./RunAsProxy.ps1 $command $arguments"
它的工作方式与您尝试的几乎相同,但使用预定义的脚本来处理高程。 但是正如您发现的那样,您不能在Start-Process
的同一调用中调用-Credential
和-Verb
。 所以这比在线定义代码更容易:
RunAsProxy.ps1
cmd.exe
并提升进程。 请注意,如果RunAsProxy.ps1
不在当前目录中,您需要提供它的相对路径或完整路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.