如果参数包含波浪号,则System.Diagnostics.ProcessStartInfo挂起

时间:2019-05-16 12:48:41

标签: git powershell escaping hang tilde

git命令git reset HEAD~将撤消上一次提交。但是,当我尝试使用System.Diagnostics.ProcessStartInfo在PowerShell中运行命令时,该过程将挂起。

我认为它与波浪号(~)有关,因为我试图用反斜杠对其进行转义,并且该命令不再挂起。但这也不起作用。它只是默默地失败了。

这是脚本:

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Program Files\Git\cmd\git.exe"
$pinfo.Arguments = "reset HEAD~"
$pinfo.WorkingDirectory = "C:\GitRepo"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.CreateNoWindow = $true

$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()

1 个答案:

答案 0 :(得分:0)

将参数作为列表传递似乎可以解决此问题:

$pinfo.Arguments = "reset","HEAD~"

我想这种方法还有更多的自动转义功能。