堆栈,
如何区分 PSObjects 是由 WriteObject() WriteWarning() WriteError() 创建的?
从此开始:
psCmd = PowerShell.Create();
Runspace = RunspaceFactory.CreateRunspace();
Runspace.Open();
psCmd.Runspace = Runspace;
psCmd.AddCommand(cmdletName);
Collection<PSObject> results = null;
results = psCmd.Invoke();
results 变量包含通过命令行输出的所有 PSObject。您如何通过命令行开关识别由 WriteObject() WriteError() WriteWarning() 创建的 PSObject?
我想添加实现以下目标的代码:
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
}
}
请您参考如下方法:
您应该能够将 Powershell 对象 (psCmd) 上的 Streams 属性用于错误和其他消息并适本地处理它们:
if (psCmd.Streams.Error.Count > 0)
{
Console.WriteLine("{0} errors", psCmd.Streams.Error.Count);
}
同样,您可以访问警告、调试、进度和详细信息。
