Hi all,
So! I’m using a runspace pool to parallelize a task. One of these gets hung up (running query.exe user /server:blah).
How do I end a PowerShell pipeline asynchronously? I see a BeginStop method, but it is a bit beyond my understanding. Has anyone used this, or would it be easy to provide an example of how to use this in PowerShell? Here is the code to reproduce the issue (leave notepad.exe up, reference $temp.powershell and $temp.runspace):
#test runspace
$scriptblock = {
param($_, $parameter)
Start-Process notepad.exe -wait
}
#init vars
$throttle = 1
$parameter = “blah”
$objectToProcess = “someServer”
#Build runspace pool
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionstate, $Host)
$runspacepool.Open()
#array for runspace tracking
$Script:runspaces = New-Object System.Collections.ArrayList
#create powershell instance and add script/args
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($objectToProcess).AddArgument($parameter)
#assign powershell instance to runspace pool
$powershell.RunspacePool = $runspacepool
#build a temp object with runspace info, add it to runspace tracking array
$temp = “” | Select-Object PowerShell, StartTime, object, Runspace
$temp.PowerShell = $powershell
$temp.StartTime = get-date
$temp.object = $objectToProcess
$temp.Runspace = $powershell.BeginInvoke()
$runspaces.Add($temp) | Out-Null
#Issue here. I want to stop a powershell pipeline, presumably using BeginStop
#$temp.powershell.BeginStop(<#this is over my head!#>)
Your insight would be greatly appreciated!