我有一个看起来像这样的批处理脚本:
测试.bat
@echo off
:: Starts a PowerShell session that starts a PowerShell process with administrator privileges
powershell -noprofile -command "&{$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;$process.WaitForExit();}"
为了便于阅读,我想将“&{ ... }”中的代码分成多行。
这篇文章说使用尾随反引号应该可以解决问题,但是当我写时:
powershell -noprofile -command "&{`
$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
$process.WaitForExit();`
}"
...也就是说,我以尾随反引号结束每一行,然后我收到以下错误:
Incomplete string token.
At line:1 char:4
+ &{` <<<<
+ CategoryInfo : ParserError: (`:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteString
'$process' is not recognized as an internal or external command,
operable program or batch file.
'$process.WaitForExit' is not recognized as an internal or external command,
operable program or batch file.
'}"' is not recognized as an internal or external command,
operable program or batch file.
我究竟做错了什么?
解决方案:
powershell -noprofile -command "&{"^
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"