AFAICSエスケープには何も問題はありませんが、あなたはどのように誤解しているのですかInvoke-Expression
。コマンドレットは、指定された文字列をコマンドラインとして実行する方法を提供します。ただし、call operator(&
)を使用しない限り、PowerShellはコマンドラインのコマンドとしてベア文字列(ケース"C:\Program Files\Git\bin\git.exe"
)を受け付けません。その代わりに、予期しないトークンに文字列をエコーしようとしたが、失敗します。
デモンストレーション:
PS C:\>"$env:windir\system32\ping.exe"
C:\Windows\system32\ping.exe
PS C:\>"$env:windir\system32\ping.exe" -n 1 127.0.0.1
At line:1 char:33
+ "$env:windir\System32\PING.EXE" -n 1 127.0.0.1
+ ~~
Unexpected token '-n' in expression or statement.
At line:1 char:36
+ "$env:windir\System32\PING.EXE" -n 1 127.0.0.1
+ ~
Unexpected token '1' in expression or statement.
At line:1 char:38
+ "$env:windir\System32\PING.EXE" -n 1 127.0.0.1
+ ~~~~~~~~~
Unexpected token '127.0.0.1' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
PS C:\>& "$env:windir\system32\ping.exe" -n 1 127.0.0.1
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
PS C:\>$cmd = "'$env:windir\system32\ping.exe' -n 1 127.0.0.1"
PS C:\>$cmd
'C:\Windows\system32\ping.exe' -n 1 127.0.0.1
PS C:\>Invoke-Expression $cmd
Invoke-Expression : At line:1 char:32
+ 'C:\Windows\system32\ping.exe' -n 1 127.0.0.1
+ ~~
Unexpected token '-n' in expression or statement.
At line:1 char:35
+ 'C:\Windows\system32\ping.exe' -n 1 127.0.0.1
+ ~
Unexpected token '1' in expression or statement.
At line:1 char:37
+ 'C:\Windows\system32\ping.exe' -n 1 127.0.0.1
+ ~~~~~~~~~
Unexpected token '127.0.0.1' in expression or statement.
At line:1 char:1
+ Invoke-Expression $cmd
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
PS C:\>$cmd = "& '$env:windir\system32\ping.exe' -n 1 127.0.0.1"
PS C:\>$cmd
& 'C:\Windows\system32\ping.exe' -n 1 127.0.0.1
PS C:\>Invoke-Expression $cmd
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
をあなただけにもすぐに呼び出し演算子を使用して(Invoke-Expression
を完全に捨てることができることを意味しますすでに提案されている@MartinBrandl)。
$gitExe = "$env:ProgramFiles\Git\bin\git.exe"
& $gitExe -c $solutionFolder
あなたは '' 'をエスケープするのを避けるために、' "'を '' '' '' ''と '' ' – sodawillow
@sodawillow私は反対を試みたが、それは働いたように見えた。 Thanks – Dov