c++ -CLI Pipeline只返回错误消息的第一行

C++-CLI Pipeline only returns the first row of the error message

本文关键字:一行 消息 Pipeline -CLI 返回 错误 c++      更新时间:2023-10-16

我正在通过代码中的管道执行exchange powershell命令。

问题是,当命令直接在powershell上执行时,返回的错误有四行长,但是当我通过管道运行命令时,它只返回错误消息的第一行。

这是我到目前为止的代码:

bool ps_calls::check_invoke(Pipeline^ pipeline)
{
    if (pipeline->Error->Count <= 0)
        return true;
    Collection<System::Object^> errorCollection =  pipeline->Error->ReadToEnd();
    for (int i=0; i< errorCollection.Count; i++)
    {
        String^ msg = String::Format("Error {0}",errorCollection[i]->ToString());
        m_errors->Add(msg);
        m_logger->info_msg(String::Format("ERROR: {0}", msg));
    }
    return false;
}

这是我的管道现在返回的一个例子:

ERROR: Error The operation couldn't be performed because object 'Testing' couldn't be found on 'server'.

下面是在powershell控制台中手动执行相同命令的错误信息的示例:

The operation couldn't be performed because object 'test' couldn't be found on 'server'.
    + CategoryInfo          : NotSpecified: (:) [Get-MailboxDatabase], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 2F753561,Microsoft.Exchange.Management.SystemConfigurationTasks.GetMailboxDatabase
    + PSComputerName        : server

我希望能够得到这个错误消息的其余部分,以及,以便我可以看到ErrorID,当错误发生

PowerShell在格式化时不调用ToString,这就是为什么你看不到相同的输出。

一些主机应用程序通常将输出管道输出到Out-Default -如果您这样做,您将看到预期的错误格式。

其他主机应用程序将输出管道输出到Out-String -这将以与管道输出到Out-Default相同的方式格式化对象,但您将获得一个字符串对象,您可以根据需要记录。