Stack,
How do you distinguish with PSObjects are created by WriteObject() WriteWarning() WriteError()?
Starting with this:
psCmd = PowerShell.Create();
Runspace = RunspaceFactory.CreateRunspace();
Runspace.Open();
psCmd.Runspace = Runspace;
psCmd.AddCommand(cmdletName);
Collection<PSObject> results = null;
results = psCmd.Invoke();
The results
variable contains the all the PSObjects piped out the commandlet. How do you identify PSObjects that were created by WriteObject() WriteError() WriteWarning() by the commandlet?
I want add code that achieves the following:
foreach(psObj in results) {
if ( IsWarning(psObj) )
{
// Turn on yellow flashing lights
}
else if ( IsError(psObj) )
{
// Turn on red flashing lights
}
else
{
// Write to ticker-tape
}
}
You should be able to use the
Streams
property on thePowershell
object (psCmd
) to the errors and other messages and handle them appropriately:Similarly, you can access warning, debug, progress and verbose.
Learn more about it here: http://msdn.microsoft.com/en-us/library/system.management.automation.psdatastreams_members(v=vs.85).ASPX