6

I have noticed that clicking the Abort button in the PowerShell ISE doesn't actually cause the background job to terminate. I guess I shouldn't be surprised since arbitrarily terminating a thread that is running unknown code could cause some pretty big problems.

However, it got me wondering... Does the abort command set some kind of flag that I could check in my script to make it support aborting cleanly? Or is there some other way to make my scripts support the abort command?

9

After currently executing "line of code" will finish its job then script execution will be immediately suspended once you click Abort.

So you don't have to control execution by your own. What is currently running must finish its work (single instruction, let's say Write-host 'test', this cannot be interrupted anywhere between).

This is more or less the answer that I got from Adam for the same question I asked him some time ago.

To proof that theory you can check how this script works:

Write-Log "Start!!!"
Start-Sleep 5
Write-Log "End!!!"

Results without stopping ISE

ManagedPoolThread #4 16:48:50 INFO  Executing a script in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session'.
ManagedPoolThread #4 16:48:50 INFO  Start!!!
ManagedPoolThread #4 16:48:55 INFO  End!!!
ManagedPoolThread #4 16:48:55 INFO  The script execution in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session' completed in 5002 ms.

Results with Abort buttong clicked

ManagedPoolThread #15 16:48:58 INFO  Executing a script in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session'.
ManagedPoolThread #15 16:48:58 INFO  Start!!!
21564 16:48:58 INFO  Aborting script execution in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session'.
ManagedPoolThread #15 16:48:58 INFO  The script execution in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session' completed in 716 ms.

EDIT: Working with pipes.

Question:

So if my script starts off with a big Get-ChildItem and pipes that to a custom function, there is no way to stop it until that pipeline is complete?

Short answer is: you still don't have to do anything additionally. Script will be suspended after the last instruction is executed.

Let's take a look on this example.

function Check-Item($i){
    Write-Log "Checking item: $($i.ID)"
    Start-Sleep 1
    $i
}

Write-Log "Start!!!"
gci -path "/sitecore/templates/branches" -r | 
    ? { $_.Name.Length -gt 0 } |
    ? { $_."__Long description" -ne $null } |
    ? { Check-Item $_ } |
    % { Write-Log $_.Name }
Write-Log "End!!!"

Notice logs:

18608 17:17:19 INFO  The script execution in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session' completed in 3 ms.
ManagedPoolThread #11 17:17:19 INFO  Executing a script in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session'.
ManagedPoolThread #11 17:17:19 INFO  Start!!!
ManagedPoolThread #11 17:17:19 INFO  Checking item: {C93C0945-607B-4916-B8D9-FE9F9BA783CA}
ManagedPoolThread #11 17:17:20 INFO  System
ManagedPoolThread #11 17:17:20 INFO  Checking item: {3E635C68-A2AA-47CD-A297-7DA9B05D56FE}
ManagedPoolThread #11 17:17:21 INFO  Analytics
ManagedPoolThread #11 17:17:21 INFO  Checking item: {07624A03-BB2F-45D8-ABE1-15E2B1705FF3}
ManagedPoolThread #11 17:17:22 INFO  Profile
ManagedPoolThread #11 17:17:22 INFO  Checking item: {67759D75-62E4-4945-8199-913BBECD1879}
ManagedPoolThread #11 17:17:23 INFO  $name
ManagedPoolThread #11 17:17:23 INFO  Checking item: {8F66D1E0-DEBA-4D68-8713-DD982C1ABDE4}
ManagedPoolThread #11 17:17:24 INFO  Profile Cards
ManagedPoolThread #11 17:17:24 INFO  Checking item: {D7192472-9456-4B1A-AE1C-6A904200E561}
20708 17:17:25 INFO  Aborting script execution in ScriptSession '$scriptSession$|qz15dry1es3umdudogfjfe0d|ISE Editing Session'.

Later you will also see error:

ManagedPoolThread #5 17:04:34 ERROR Error while executing GetChildItems(string path='master:/sitecore/templates', string recurse='True')
Exception: System.Management.Automation.PipelineStoppedException
Message: The pipeline has been stopped.

But it is handled in code, just logged.

| improve this answer | |
  • 1
    So if my script starts off with a big Get-ChildItem and pipes that to a custom function, there is no way to stop it until that pipeline is complete? – Ben Golden Oct 19 '16 at 14:54
  • @ben-golden I just edited my answer with additional information. – Alan Płócieniak Oct 19 '16 at 15:13
  • Testing with a simple script like the one you provided does indeed show the behavior you describe, so I'll accept it as the answer. However, my real script seems to run to completion after I have clicked the abort button. I have also noticed that the UI seems to get disconnected from the background job sometimes with very long-running scripts, so perhaps that has something to do with it. – Ben Golden Oct 19 '16 at 15:45
  • Your are probably right. I am using the most recent version of SPE for tests. UI freeze isn't something normal or I never did such a big processing to experience it. – Alan Płócieniak Oct 19 '16 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.