Reputation: 1713
I'm executing exchange powershell commands through a pipeline from my code.
The problem is that when the command is executed on the powershell directly the error returned is four rows long, but when I run the command through the pipeline it only returns the first row of the error message.
This is the code I have so far:
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;
}
This is an example of what my pipeline returns now:
ERROR: Error The operation couldn't be performed because object 'Testing' couldn't be found on 'server'.
This is an example of what the error message is if the same command is executed on the powershell console manually:
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
I would like to be able to get the rest of this error message as well so that I can see the ErrorID when an error occurs.
Upvotes: 0
Views: 184
Reputation: 8019
PowerShell doesn't call ToString when it does formatting, that's why you don't see the same output.
Some host applications typically pipe output to Out-Default - if you did that, you'd see the expected format for errors.
Other host applications will pipe output to Out-String - this will format objects in the same way that the would be formatted if piped to Out-Default, but you'll get a string object that you can log however you need to.
Upvotes: 1