2012-06-20 10 views
9

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();"^ 
"}" 

risposta

6

Puoi provare il cursore ^ per indicare che la linea di corrente continua su quello successivo.

powershell -noprofile -command "&{"^ 
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^ 
"$process.WaitForExit();"^ 
"}" 

Si prega di notare anche lo spazio iniziale così come la chiusura e l'apertura " su ogni linea.

Vedi anche Long commands split over multiple lines in Windows Vista batch (.bat) file

+0

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. " –

+0

Prova ad aggiungere uno spazio all'inizio delle seguenti righe. – marapet

+0

Non ha funzionato, ha ancora lo stesso errore. :/ –

1

Prova questo credo che questo aiuterà

powershell -noprofile -command ""&{^ 
$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb 
RunAs -PassThru;^ 
$process.WaitForExit();^ 
}"" 

Let me know Grazie

+0

Impossibile farlo funzionare. –

Problemi correlati