Ho uno script batch che assomiglia a questo:PowerShell -Command su più righe
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();}"
Vorrei dividere il codice all'interno del "& {...} "su più righe per la leggibilità.
This Post dice che l'utilizzo di un apice inverso finale dovrebbe fare il trucco, ma quando scrivo:
powershell -noprofile -command "&{`
$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
$process.WaitForExit();`
}"
... cioè, io alla fine di ogni riga con un backtick finale, allora ottengo il seguente errore :
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.
Qualcuno può dirmi cosa sto facendo male?
SOLUZIONE:
powershell -noprofile -command "&{"^
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"
che si traduce in: ". Chiusura mancante '}' nel blocco di istruzioni Adiacente : 1 char: 4 + & {^ <<<< + CategoriaInfo: Errore Parser: (CloseBraceToken: TokenId) [], ParentContainsErrorRecord Eccezione + FullyQualifiedErrorId: MissingEndCurlyBrace '$ processo' non è riconosciuto come comando interno o esterno, programma operativo o file batch. " –
Prova ad aggiungere uno spazio all'inizio delle seguenti righe. – marapet
Non ha funzionato, ha ancora lo stesso errore. :/ –