简体   繁体   English

如果没有System.Management.Automation.Powershell.Invoke(),则声明HadErrors

[英]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. 我在使用以下代码时遇到问题,该代码在具有Powershell 1-4版本的计算机上可以正常工作,但是在具有Powershell V5的计算机上运行时,测试“ posh.HadErrors”返回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. 我尝试运行的任何git命令,在安装了v5的计算机上运行时,powershell都声称有错误。 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. 即使没有错误,git也会输出到stderr。

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以查看异常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Management.Automation中没有PowerShell? - No PowerShell in System.Management.Automation? C#System.Management.Automation.Powershell类在Invoke()调用时泄漏内存 - C# System.Management.Automation.Powershell class leaking memory on Invoke() call System.Management.Automation.Powershell RunAs管理员 - System.Management.Automation.Powershell RunAs Admin System.Management.Automation.VerbsCommon中的“调用”成员在哪里? - Where is the “Invoke” member in System.Management.Automation.VerbsCommon? 如何向 powershell 脚本 (System.Management.Automation.Powershell) 提供参数? - How to supply parameters to powershell script (System.Management.Automation.Powershell)? 使用 System.Management.Automation 调用 powershell 时,如何从远程命令传递警告和详细流? - How to pass Warning and Verbose streams from a remote command when calling powershell using System.Management.Automation? 发生异常:运行简单 PowerShell 命令时出现 CLR/System.Management.Automation.Runspaces.PSSnapInException - Exception has occurred: CLR/System.Management.Automation.Runspaces.PSSnapInException when running simple PowerShell command C#中的PowerShell System.Management.Automation.Runspaces与TAP - PowerShell System.Management.Automation.Runspaces vs TAP in C# Windows Powershell SDK和System.Management.Automation.PSObject - Windows Powershell SDK and System.Management.Automation.PSObject MVC System.Management.Automation Powershell执行中的访问被拒绝错误 - Access Denied error in MVC System.Management.Automation powershell execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM