How to stop asynchronous script block PowerShell?

I am trying to get a Powershell script to start the asynchronous script block loop and then do some “other things”. When the "other stuff" is completed at some unknown late time, I want to stop / kill the script block.

I see MS dev network information by the method stopasych(), but I can’t figure out how to apply it in Powershell.

A simple example:

$runspace = [runspacefactory]::CreateRunspace()
$ps = [powershell]::create()
$ps.runspace = $runspace

[void]$ps.addscript({
   $beep = "`a"
   for ($i = 1; $i -lt 5000; $i++) {
      $beep
      Start-Sleep -Seconds 10
   }
})
$runspace.open()
$asyncObject = $ps.beginInvoke()

Copy-Item "c:\bigdata" "\\server\share"

It makes me start, but when copying is finished, I want to kill the beep. I can’t undo them (that is, “copy” in the background) because it ends with a delay until the condition $ps.InvocationStateInfo.stateis checked again.

Can anyone help? By the way, I would like to stick with PoSh v2.

+4
1

, Invoke() Begin/End, Stop().

, :

$ps.EndStop($ps.BeginStop($null,$asyncObject))
+1

Source: https://habr.com/ru/post/1678800/


All Articles