【发布时间】:2021-01-22 22:31:07
【问题描述】:
示例脚本如下所示:
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)
【问题讨论】:
-
无法重现...我看到所有三个输出
标签: .net vb.net powershell