Skip to content

Instantly share code, notes, and snippets.

Created April 17, 2017 23:10
Show Gist options
  • Save anonymous/d35535d7ade19294461c1bf16a456426 to your computer and use it in GitHub Desktop.
Save anonymous/d35535d7ade19294461c1bf16a456426 to your computer and use it in GitHub Desktop.
Self-cleaning jobs make more jobs to clean themselves up. Recursively?
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