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();"^
"}"
Đ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. " –
Thử thêm một khoảng trống ở đầu các dòng sau. – marapet
Không hoạt động, vẫn gặp lỗi tương tự. :/ –