web-dev-qa-db-ja.com

空白のあるパスを含む引数でプロセスを開始します

私はpowershellスクリプトからプロセスを開始し、そのようなparamsを渡す必要があります:-a -s f1d:\ some directory \に空白を含むpath\file.issにそれを行うには、次のコードを書きます:

$process = [System.Diagnostics.Process]::Start("$setupFilePath", '-a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"') 
$process.WaitForExit()

その結果、プロセスは開始されますが、最後の引数:-f1d:\ some directory \にpath\file.issの空白が正しく渡されていません。助けてください

12
vklu4itesvet

Start-Processを使用できると思います:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s','-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' |
    Wait-Process
9
Aaron Jensen

私はあなたの質問を理解しています:複数の引数を渡して、引数の1つにスペースがあるプロセスを開始する方法は?

私はWindowsバッチファイルで同等のものが次のようなものになると仮定しています:

"%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

ここで、二重引用符は、受信プロセス(この場合はsetupFilePath)が3つの引数を受信できるようにします。

  1. -a
  2. -s
  3. -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

あなたの質問のコードスニペットでこれを実現するには、1の左側とエスケープキーの下にバックティック(を使用します。一重引用符と混同しないでください、別名Grave-accent)このように内側の二重引用符をエスケープするには:

$process = [System.Diagnostics.Process]::Start("$setupFilePath", "-a -s -f1`"d:\some directory\with blanks in a path\fileVCCS.iss`"") 
$process.WaitForExit()

バックティックの使用に加えて、引数リストを囲む一重引用符も二重引用符に変更したことに注意してください。ここで必要なエスケープは一重引用符では許可されないため、これが必要でした( http://ss64.com/ps/syntax-esc.html )。

アーロンの答え は問題なく動作するはずです。そうでない場合は、setupFilePath-f1"d:\space here\file.ext"期待どおり。

意見アラート彼の答えに追加する唯一のことは、引数のパス内で変数を使用できるようにするために、二重引用符と逆ティックを使用することを提案することです-f1

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
Wait-Process

この方法では、長い行の途中にハードコーディングされた絶対パスがありません。

5
Benrobot

PowerShell v3では、これは機能します。

& $setupFilePath -a -s -f1:"d:\some directory\with blanks in a path\fileVCCS.iss"

[〜#〜] pscx [〜#〜] echoargsコマンドを使用すると、次のように表示されます。

25> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s "-f1d:\some directory\with blanks in a path\fileVCCS.iss"

V2を使用する場合-最後の二重引用符にバックティックが追加されていることに注意してください。

PS> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss`"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
2
Keith Hill