2012-06-20 18 views
9

que tiene una secuencia de comandos por lotes que se parece a esto:Powershell -Command al borde múltiple

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

me gustaría dividir el código dentro de la "& {...} "sobre líneas múltiples para legibilidad.

This Post dice que el uso de un acento grave de arrastre debe hacer el truco, pero cuando escribo:

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

... es decir, termino cada línea con un acento grave se arrastra, entonces me sale el siguiente error :

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. 

¿Alguien me puede decir lo que estoy haciendo mal?

SOLUCIÓN:

powershell -noprofile -command "&{"^ 
"$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^ 
"$process.WaitForExit();"^ 
"}" 

Respuesta

6

Usted puede tratar el símbolo de intercalación ^ para indicar que la línea actual continúa en la siguiente.

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

Tenga en cuenta también el espacio inicial, así como el cierre y apertura " en cada línea.

Ver también Long commands split over multiple lines in Windows Vista batch (.bat) file

+0

que se traduce en:. "Cierre Missing '}' en el bloque de instrucciones En línea : 1 char: 4 + & {^ <<<< + CategoryInfo: ParserError: (CloseBraceToken: TokenId) [], ParentContainsErrorRecord Excepción + FullyQualifiedErrorId: MissingEndCurlyBrace '$ process' no se reconoce como un comando interno o externo, programa operable o archivo por lotes. " –

+0

Intenta agregar un espacio al comienzo de las siguientes líneas. – marapet

+0

No funcionó, igual recibió el mismo error. :/ –

1

Prueba esto creo que esto ayudará

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

Avísame Gracias

+0

No se pudo hacer funcionar esto. –

Cuestiones relacionadas