Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Management.Automation.Powershell.Invoke()在没有错误时声明有错误_C#_Git_Powershell_Command Line - Fatal编程技术网

C# System.Management.Automation.Powershell.Invoke()在没有错误时声明有错误

C# System.Management.Automation.Powershell.Invoke()在没有错误时声明有错误,c#,git,powershell,command-line,C#,Git,Powershell,Command Line,我对以下代码有一个问题,它在具有Powershell版本1-4的计算机上运行良好,但在具有Powershell V5的计算机上运行时,“posh.haderors”测试返回true。我在一个干净的vagrant安装上尝试了这个,所以它不是一个配置文件或环境变量问题。任何我尝试运行的git命令,powershell都声称在安装了v5的计算机上运行时出错。我甚至不知道如何调试这个问题 using (var posh = PowerShell.Create()) { string result

我对以下代码有一个问题,它在具有Powershell版本1-4的计算机上运行良好,但在具有Powershell V5的计算机上运行时,“posh.haderors”测试返回true。我在一个干净的vagrant安装上尝试了这个,所以它不是一个配置文件或环境变量问题。任何我尝试运行的git命令,powershell都声称在安装了v5的计算机上运行时出错。我甚至不知道如何调试这个问题

using (var posh = PowerShell.Create())
{
    string resultString = "";
    posh.AddScript("git checkout master");
    var results = posh.Invoke();
    foreach (var result in results)
    {
        if (result == null) { continue; }

        if (posh.HadErrors)
        {
            // This is getting hit when it shouldn't when running Powershell V5
            _console.LogError(result.ToString());
        }
        else
        {
            resultString += result + "\r\n";
            _console.LogDebug(result.ToString());
        }
    }

    foreach (var error in posh.Streams.Error)
    {
        _console.LogError(error.ToString());
    }

    return Tuple.Create(!posh.HadErrors, resultString);
}

即使没有错误,git也会输出到stderr

例如,如果它看起来像这样:

> git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
git checkout master 1>output.log 2>error.log
第一行是错误输出

如果是这样的话:

> git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
git checkout master 1>output.log 2>error.log
第一行仍然是错误输出

您可以通过如下方式运行命令进行验证:

> git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
git checkout master 1>output.log 2>error.log

Id HadErroes equal true,请检查PowerShellInstance.InvocationStateInfo.Reason以查看异常

错误消息是什么?也许那台机器上的git有问题?也许
git
返回非零退出代码?
git
是否在路径中?当前目录是git repo吗?如果您在Quick watch中检查
posh
,是否发现任何可疑信息?@cezarypatek没有错误消息,结果包含您通常期望的内容(例如“切换到分支主控”)$LASTEXITCODE是0。@wOxxOm Git在路径中,就像我说的,它正确地执行了命令,只是错误地报告它有错误。