Hi
I use code such as the (simplified) example below to run a PowerShell command in my VB app using System.Management.Automation.
I need to be able to add the ability to cancel a long running operation once it has started, does the below support the use of CancellationToken or similar, if so how? or if not how can I modify the below so if I click a cancel button on my form it would stop the operation?
Thanks
Private Async Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Dim computers As New List(Of String)({"PC1", "PC2", "PC3"}) ' this is just example, real code could have hundreds of computers
Using ps As PowerShell = PowerShell.Create()
ps.AddCommand("Test-Connection").AddParameter("ComputerName", computers).AddParameter("Count", 1)
Dim output As New PSDataCollection(Of PSObject)()
AddHandler output.DataAdded, AddressOf Output_DataAdded
AddHandler ps.Streams.Error.DataAdded, AddressOf Error_DataAdded
Dim results As PSDataCollection(Of PSObject) = Await Task.Run(Function() ps.InvokeAsync(Of PSObject, PSObject)(Nothing, output))
End Using
End Sub