Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未调用.NET PowerShell写入错误流事件处理程序_C#_Powershell - Fatal编程技术网

C# 未调用.NET PowerShell写入错误流事件处理程序

C# 未调用.NET PowerShell写入错误流事件处理程序,c#,powershell,C#,Powershell,我是用C代码打电话的。我将EventHandler添加到powershellInstance.Streams.Error.DataAdded中,但从未调用它,而且执行后的powershellInstance.Streams.Error.Count始终为0。我知道应该有错误的原因是我手动执行相同的命令,并且可以看到错误打印出来 查看模块的详细信息,他们使用了写入错误,因此我不明白为什么我不能捕获此信息,我可以捕获写入警告,-调试,-进度,以及-详细 有什么建议吗 下面是我的PowerShell C

我是用C代码打电话的。我将
EventHandler
添加到
powershellInstance.Streams.Error.DataAdded
中,但从未调用它,而且执行后的
powershellInstance.Streams.Error.Count
始终为0。我知道应该有错误的原因是我手动执行相同的命令,并且可以看到错误打印出来

查看模块的详细信息,他们使用了
写入错误
,因此我不明白为什么我不能捕获此信息,我可以捕获
写入警告
-调试
-进度
,以及
-详细

有什么建议吗

下面是我的PowerShell C#代码:

private System.Management.Automation.PowerShell powershellInstance=null;
公共异步任务执行(
字符串脚本,
CancellationToken CancellationToken,
ExecutionPolicy ExecutionPolicy=ExecutionPolicy.Default,
IList listParams=null,
IDictionary pairParams=null,
EventHandler verboseHandler=null,
EventHandler-debugHandler=null,
EventHandler warningHandler=null,
EventHandler errorHandler=null,
EventHandler progressHandler=null)
{
powershellInstance=System.Management.Automation.PowerShell.Create();
LastOutputCollection=新PSDataCollection();
powershellInstance.AddCommand(“设置执行策略”);
powershellInstance.AddParameter(“ExecutionPolicy”,ExecutionPolicy.ToString());
powershellInstance.AddParameter(“Force”);
powershellInstance.AddScript(脚本);
if(listParams!=null&&listParams.Count>0)
{
powershellInstance.AddParameters(listParams);
}
if(pairParams!=null&&pairParams.Count>0)
{
powershellInstance.AddParameters(pairParams);
}
if(verboseHandler!=null)
{
powershellInstance.Streams.Verbose.DataAdded+=verboseHandler;
}
if(debugHandler!=null)
{
powershellInstance.Streams.Debug.DataAdded+=debugHandler;
}
if(warningHandler!=null)
{
powershellInstance.Streams.Warning.DataAdded+=warningHandler;
}
if(errorHandler!=null)
{
powershellInstance.Streams.Error.DataAdded+=errorHandler;
}
if(progressHandler!=null)
{
powershellInstance.Streams.Progress.DataAdded+=progressHandler;
}
return wait Task.Factory.StartNew(()=>
{
PSDataCollection结果=null;
var actionResult=powershellInstance.BeginInvoke(
null/*输入*/,,
最后输出(收集);
WaitHandle.WaitAny(新[]{actionResult.AsyncWaitHandle,cancellationToken.WaitHandle});
if(cancellationToken.IsCancellationRequested)
{
powershellInstance.Stop();
}
其他的
{
InvokeCallback(actionResult);
结果=powershellInstance.EndInvoke(actionResult);
}
powershellInstance.Dispose();
返回结果;
});
}
public void InvokeCallback(IAsyncResult asyncResult)
{
尝试
{
logger.Info(“调用了Powershell回调。AsyncResult已完成={5}.{0}错误。{1}警告。{2}调试。{3}进度。{4}详细。”,
powershellInstance.Streams.Error.Count,
powershellInstance.Streams.Warning.Count,
powershellInstance.Streams.Debug.Count,
powershellInstance.Streams.Progress.Count,
powershellInstance.Streams.Verbose.Count,
asyncResult.IsCompleted
);
}
捕获(例外情况除外)
{
FatalException(“InvokeCallback中的异常”,ex);
}
}
下面是我的错误处理程序:

    public void PSErrorHandler(object sender, DataAddedEventArgs arg)
    {
        log.Info($"PSErrorHandler called. Sender type is {sender.GetType()}");
        PSDataCollection<ErrorRecord> records = sender as PSDataCollection<ErrorRecord>;
        ErrorRecord record = records[arg.Index];
        LogError($"PSError: ErrorId: {record.FullyQualifiedErrorId}. Message: {record.ErrorDetails.Message}; Recommendation: {record.ErrorDetails.RecommendedAction}.").Wait();
        LogError($"PSError-Exception: Message: {record.Exception.Message}; Source: {record.Exception.Source}; Trace: {record.Exception.StackTrace}.").Wait();
    }
public void pErrorHandler(对象发送方,DataAddedEventArgs arg)
{
log.Info($”PSErrorHandler调用的.Sender类型为{Sender.GetType()}”);
PSDataCollection记录=发送方作为PSDataCollection;
ErrorRecord=记录[参数索引];
LogError($“PSError:ErrorId:{record.FullyQualifiedErrorId}。消息:{record.ErrorDetails.Message};建议:{record.ErrorDetails.RecommendeAction}。)。等待();
LogError($“PSError异常:消息:{record.Exception.Message};源:{record.Exception.Source};跟踪:{record.Exception.StackTrace}”)。等待();
}

手动运行PowerShell时,您看到打印出来的错误是什么?另外,PS脚本中是否以任何方式内置了错误处理功能?你能提供那个脚本吗,还是太长/太专有?你实际上是如何使用
Execute
方法的?你能构造吗?
    public void PSErrorHandler(object sender, DataAddedEventArgs arg)
    {
        log.Info($"PSErrorHandler called. Sender type is {sender.GetType()}");
        PSDataCollection<ErrorRecord> records = sender as PSDataCollection<ErrorRecord>;
        ErrorRecord record = records[arg.Index];
        LogError($"PSError: ErrorId: {record.FullyQualifiedErrorId}. Message: {record.ErrorDetails.Message}; Recommendation: {record.ErrorDetails.RecommendedAction}.").Wait();
        LogError($"PSError-Exception: Message: {record.Exception.Message}; Source: {record.Exception.Source}; Trace: {record.Exception.StackTrace}.").Wait();
    }