In reference to using PowerShell in VB or C# via System.Management.Automation
I know that Invoke runs synchronously, and using BeginInvoke / EndInvoke means you can do same thing asynchronously.
But I noticed today that there is also InvokeAsync
Can anyone explain to me the benefit of using this over the other methods and if there is a particular method I should be using?
Most examples I see use Invoke for basic commands and BeginInvoke / EndInvoke for longer running tasks.
Just want to learn best approach and have an understanding of what to use when as can't find much documentation on InvokeAsync like I could the others.
update: Quick examples below of same code using Invoke or BeginInvoke/EndInvoke
Thanks
Dim powershell As PowerShell = PowerShell.Create()
powershell.AddCommand("Get-Process")
Dim results = powershell.BeginInvoke
Do While results.IsCompleted = False
' wait whilst command completes
Loop
For Each result As PSObject In powershell.EndInvoke(results)
UpdateTextBox("Process Name: " & result.Members("ProcessName").Value & " Process ID: " & result.Members("Id").Value & vbCrLf)
Next result
or
Dim powershell As PowerShell = PowerShell.Create()
powershell.AddCommand("Get-Process")
Dim results = powershell.Invoke
For Each result As PSObject In results
UpdateTextBox("Process Name: " & result.Members("ProcessName").Value & " Process ID: " & result.Members("Id").Value & vbCrLf)
Next result