带有-ArgumentList类型命令的`Start-ProcessPowerShell`不调用命令
我在 powershell 中执行以下几行:
$argList = "-NoExit -NoProfile -Command {Write-Host 'hello world'}";
Start-Process PowerShell -ArgumentList $argList;
我想要的输出是创建一个新的 powershell windows 并输出hello world
.
但是我得到的是打开一个新的 powershell 窗口并像Write-Host 'hello world'
. 所以在新窗口中实际上并没有执行Write-Host。如何解决这个问题?
回答
问题在于您将脚本块作为文本插入的 $argList 字符串的引用。
尝试以下任一方法:
$argList = "-NoExit -NoProfile `"Write-Host 'hello world'`"" # works
$argList = "-NoExit -NoProfile -Command `"Write-Host 'hello world'`"" # works
$arglist = '-NoExit', '-NoProfile', '-Command', 'Write-Host "hello world"' # works
$arglist = '-NoExit', '-NoProfile', '-Command', {Write-Host "hello world"} # works
$argList = '-NoExit -NoProfile -Command', {Write-Host "hello world"} # works
$argList = '-NoExit -NoProfile -Command', 'Write-Host "hello world"' # works
Start-Process PowerShell -ArgumentList $argList
THE END
二维码