Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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# 在C中不总是看到PowerShell警告流的输出#_C#_Powershell - Fatal编程技术网

C# 在C中不总是看到PowerShell警告流的输出#

C# 在C中不总是看到PowerShell警告流的输出#,c#,powershell,C#,Powershell,我正试图通过我的C#应用程序在PowerShell中运行以下命令 PsSession.RunPowerShellScript("Get-Mailboxstatistics -Identity John.Doe"); 方法RunPowerShellScript返回输出的串联字符串,如下所示 public static string RunPowerShellScript(string psScript) { try { PowerShell ps = PowerS

我正试图通过我的C#应用程序在PowerShell中运行以下命令

PsSession.RunPowerShellScript("Get-Mailboxstatistics -Identity John.Doe");
方法
RunPowerShellScript
返回输出的串联字符串,如下所示

public static string RunPowerShellScript(string psScript)
{
    try
    {
        PowerShell ps = PowerShell.Create();
        ps.Runspace = psRunSpace; //Set Statically Elsewhere
        ps.AddScript(psScript);

        Collection<PSObject> results = ps.Invoke();

        if (ps.HadErrors)
        {
            string output = "";
            foreach (ErrorRecord errorRecord in ps.Streams.Error)
                output += errorRecord.Exception.Message.ToString();
            throw new Exception(output);
        }
        else
        {
            if (results.Count != 0)
            {
                string output = "";
                foreach (PSObject result in results)
                    output += result.BaseObject.ToString();
                return output;
            }
            else
            {
                if (ps.Streams.Warning.Count != 0)
                {
                    string output = "";
                    foreach (WarningRecord s in ps.Streams.Warning)
                        output += s.Message.ToString();
                    return output;
                }
                else
                    return "No Results.";
            }
        }
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message.ToString();
    }
}
然后它会正确返回警告(“测试”)

我尝试通过运行以下命令将警告流重定向到输出流

PsSession.RunPowerShellScript("Get-Mailboxstatistics -Identity John.Doe  *>&1");
但这并不能解决问题

如果从PowerShell ISE运行cmdlet,我会得到以下结果

PS C:\Windows\system32> Get-Mailboxstatistics -Identity John.Doe
WARNING: The user hasn't logged on to mailbox 'John.Doe'...

我也遇到过同样的问题,我决定使用-WarningAction Stop,然后解析异常消息。

你在脚本中使用隐式远程处理吗?我可能不熟悉这个术语,但在谷歌搜索之后,我会说我认为我是。我正在通过创建会话然后导入会话连接到Exchange Online。(这在上面的psRunSpace对象中,我的想法是我可以创建一次会话,然后根据需要使用它。)我遇到了在该环境中捕获警告和错误的问题。-WarningVariable和-ErrorVariable公共参数也不起作用。我发现的唯一解决方法是使用Start Transcript,然后从Transcript文件中解析警告消息。我怀疑这与exchange会话的NoLanguage约束以及您不是真正运行cmdlet而是代理函数有关。这是否只会影响警告流?当有错误时,错误流会被填充。如果你已经找到了这个问题的答案,请你自己发布一个答案块。人们想知道。谢谢
PS C:\Windows\system32> Get-Mailboxstatistics -Identity John.Doe
WARNING: The user hasn't logged on to mailbox 'John.Doe'...