我正在通过代码中的管道执行exchange powershell命令。
问题是,在powershell上直接执行命令时,返回的错误是4行,但是当我通过管道运行命令时,它只返回错误消息的第一行。
这是我到目前为止掌握的代码:
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。
发布于 2014-03-18 16:48:40
PowerShell在格式化时不调用ToString,这就是为什么您看不到相同的输出。
一些主机应用程序通常将输出输送到外部默认值--如果这样做,您将看到预期的错误格式。
其他主机应用程序将输出管道输出到Out-String -这将以同样的方式格式化对象,如果管道被格式化为Out-默认值,但您将得到一个字符串对象,您可以登录,无论您需要。
https://stackoverflow.com/questions/22481228
复制相似问题