2012-06-20 31 views
9

Tôi có một kịch bản hàng loạt trông như thế này:Powershell -Command trên nhiều dòng

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

Tôi muốn chia tay các mã bên trong "& {...} "trên nhiều dòng cho khả năng đọc.

This bài nói rằng việc sử dụng một backtick dấu nên làm các trick, nhưng khi tôi viết:

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

... có nghĩa là, tôi kết thúc mỗi dòng với một backtick dấu, sau đó tôi nhận được lỗi sau :

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. 

Bất cứ ai có thể cho tôi biết tôi đang làm gì sai?

SOLUTION:

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

Trả lời

6

Bạn có thể thử các caret ^ để chỉ ra rằng dòng hiện tại vẫn diễn ra trên kế tiếp.

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

Cũng xin lưu ý không gian hàng đầu cũng như đóng và mở " trên mỗi dòng.

Xem thêm Long commands split over multiple lines in Windows Vista batch (.bat) file

+0

Điều đó dẫn tới:. "Thiếu đóng cửa '}' trong khối tuyên bố Tại dòng : 1 char: 4 + & {^ <<<< + CategoryInfo: ParserError: (CloseBraceToken: TokenId) [], ParentContainsErrorRecord Ngoại lệ + FullyQualifiedErrorId: MissingEndCurlyBrace '$ process' không được công nhận là lệnh nội bộ hoặc bên ngoài, chương trình có thể hoạt động hoặc tệp hàng loạt. " –

+0

Thử thêm một khoảng trống ở đầu các dòng sau. – marapet

+0

Không hoạt động, vẫn gặp lỗi tương tự. :/ –

1

Hãy thử điều này tôi nghĩ rằng điều này sẽ giúp

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

Hãy cho tôi biết Cảm ơn

+0

Không thể làm việc này. –

Các vấn đề liên quan