PowerShell-Befehl über mehrere Zeilen
Ich habe ein Batch-Skript, das so aussieht:
Test.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();}"
Ich möchte den Code innerhalb des "& {...}" zur besseren Lesbarkeit über mehrere Zeilen aufteilen.
Diese post sagt, dass die Verwendung eines nachgestellten Backtick den Trick tun sollte, aber wenn ich schreibe:
powershell -noprofile -command "&{`
$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
$process.WaitForExit();`
}"
... das heißt, ich beende jede Zeile mit einem abschließenden Backtick, dann erhalte ich den folgenden Fehler:
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.
Was mache ich falsch?
LÖSUNG:
powershell -noprofile -command "&{"^
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"