.net PowerShell主机->;未引发进程事件
示例脚本如下所示:.net PowerShell主机->;未引发进程事件,.net,vb.net,powershell,.net,Vb.net,Powershell,示例脚本如下所示: Begin { Write-Output "Begin block processed." } Process { Write-Output "Process block processed." } End { Write-Output "End block processed." } 通过.NET中的System.Automation.PowerShell主机运行此操作时,输出仅为:
Begin
{
Write-Output "Begin block processed."
}
Process
{
Write-Output "Process block processed."
}
End
{
Write-Output "End block processed."
}
通过.NET中的System.Automation.PowerShell主机运行此操作时,输出仅为:
Begin blocked processed.
End blocked processed.
知道为什么进程块没有被处理吗
基本上,背后的代码是:
Dim ps As PowerShell = PowerShell.Create()
ps.AddScript(strScriptText)
ps.Invoke(Nothing, outputCollection)
更新:问题已经解决。问题是事情的顺序
该命令在以下情况下有效:
AddHandler outputCollection.DataAdded, AddressOf OutputDataReceived
AddHandler ps.Streams.Error.DataAdded, AddressOf ErrorDataReceived
AddHandler ps.InvocationStateChanged, AddressOf InvocationStateChanged
ps.AddScript(strScript)
ps.AddCommand("Out-String").AddParameter("Stream")
ps.Invoke(Nothing, outputCollection)
此命令不起作用:
AddHandler outputCollection.DataAdded, AddressOf OutputDataReceived
AddHandler ps.Streams.Error.DataAdded, AddressOf ErrorDataReceived
AddHandler ps.InvocationStateChanged, AddressOf InvocationStateChanged
ps.AddCommand("Out-String").AddParameter("Stream")
ps.AddScript(strScript)
ps.Invoke(Nothing, outputCollection)
根据:
在管道中,进程块对到达函数的每个输入对象执行一次
在您的代码中,您可以发送Nothing
作为输入对象。在visual basic中,这相当于发送一个空的IEnumerable
对象。由于IEnumerable对象为空,因此进程块没有要处理的项。因此,不执行进程块,而无论处理多少项,开始和结束块始终执行一次
如果发送一个
IEnumerable
对象,例如列表
,而不是Nothing
关键字,则进程块将执行一次。试试看。无法重新编程。。。我看到了所有三个输出信号,但不是它。我更新了原来的帖子。是顺序AddScript/AddCommand造成了问题。