简体   繁体   中英

System.Management.Automation.Powershell.Invoke() claims HadErrors when there were none

I'm having an issue with the following code where it works fine on computers who have Powershell version 1-4 but when ran on a computer with Powershell V5, the test, "posh.HadErrors" is returning true. I tried this on a clean vagrant install so it's not a profile or env var issue. Any git command I try running, powershell claims to have errors when running on a computer with v5 installed. I'm not even sure how to debug this issue.

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 outputs to stderr even when there's no error.

Eg if it looks like this:

> git checkout master
Already on 'master'
Your branch is up-to-date with 'origin/master'.

That first line is error output.

If it looks like this:

> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

That first line is still error output.

You can verify it by running the command like this:

git checkout master 1>output.log 2>error.log

如果HadErroes等于true,请检查PowerShellInstance.InvocationStateInfo.Reason以查看异常

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM