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.