Jan*_*eng 9 powershell batch-file
我有一个如下所示的批处理脚本:
@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();}"
Run Code Online (Sandbox Code Playgroud)
为了便于阅读,我想将"&{...}"内的代码拆分为多行.
这篇文章说使用尾随反引用应该可以解决问题,但是当我写下:
powershell -noprofile -command "&{`
$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
$process.WaitForExit();`
}"
Run Code Online (Sandbox Code Playgroud)
...也就是说,我用尾随反引号结束每一行,然后我收到以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
解:
powershell -noprofile -command "&{"^
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用插入符号^
来指示当前行继续下一行:
powershell -noprofile -command "&{"^
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"
Run Code Online (Sandbox Code Playgroud)
请注意领先的空间以及"
每条线的关闭和开口.
另请参阅在Windows Vista批处理(.bat)文件中拆分多行的长命令.