12

次のようなバッチ スクリプトがあります。

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

読みやすくするために、「&{ ... }」内のコードを複数行に分割したいと思います。

This postは、末尾のバックティックを使用するとうまくいくはずだと言っていますが、私が書くとき:

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

...つまり、各行を末尾のバッククォートで終了すると、次のエラーが発生します。

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.

私は何を間違っていますか?

解決:

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

2 に答える 2

10

^現在の行が次の行に続くことを示すために、キャレットを試すことができます。

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

"各行の開始と終了だけでなく、先頭のスペースにも注意してください。

Windows Vista バッチ (.bat) ファイル内の複数行に分割された長いコマンドも参照してください。

于 2012-06-20T12:17:41.383 に答える
1

以下を試してください。これが役立つと思います。

powershell -noprofile -command ""&{^
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb
    RunAs -PassThru;^
    $process.WaitForExit();^
}""
于 2012-06-20T12:30:47.410 に答える