A sample script looks like this:
Begin
{
Write-Output "Begin block processed."
}
Process
{
Write-Output "Process block processed."
}
End
{
Write-Output "End block processed."
}
When running this through System.Automation.PowerShell host in .NET the output is only:
Begin blocked processed.
End blocked processed.
Any idea why the Process block is not processed.
Basically, the code behind is:
Dim ps As PowerShell = PowerShell.Create()
ps.AddScript(strScriptText)
ps.Invoke(Nothing, outputCollection)
UPDATE: The issue has been fixed. The problem was the order of things.
This order does work:
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)
This order does not work:
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)
question from:
https://stackoverflow.com/questions/65853599/powershell-host-process-event-not-raised 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…