Created
April 17, 2017 23:10
-
-
Save anonymous/d35535d7ade19294461c1bf16a456426 to your computer and use it in GitHub Desktop.
Self-cleaning jobs make more jobs to clean themselves up. Recursively?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-SelfCleaningJob { | |
param( | |
$Name=$([Guid]::NewGuid().Guid) | |
) | |
$job = Start-Job -Name $Name { 1..10 | % { Write-Output $_ } } | |
$jobCleanup = Register-ObjectEvent $job -EventName StateChanged -Action { | |
if($EventArgs.JobStateInfo.State -eq "Completed") { | |
# Note this prints to host no matter what's going on right now .... | |
Receive-Job $Sender | out-host | |
# Clean up and throw away the job | |
$Sender | Stop-Job -Passthru | Remove-Job | |
} | |
} | |
# But how can I clean up the clean up job? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment