Say I have a file A.ps1
that calls B.ps1
:
& "B.ps1"
I want the contents of B.ps1 to run after N seconds, without blocking A.ps1. In this case, A.ps1 would finish immediately, and then the contents of B.ps1 would run after a set time.
How can this be achieved?
Context
We're leveraging Release Management to deploy builds with a powershell script. Sometimes RM outputs logging data that we need into a IR_ProcessAutoOutput
file - but this only gets generated once RM completes. So I want to defer execution of the "GetLogs" script for ~20 seconds without blocking, allowing RM to complete and generate the IR_ProcessAutoOutput in the meantime.
Use Start-Process
instead of the call operator.
Start-Process 'powershell.exe' -ArgumentList '-File', 'B.ps1'
If you don't want the process to run in a different window add the parameter -NoNewWindow
.
You could also run the second script as a background job:
Start-Job -Scriptblock { & 'B.ps1' }
If you want B.ps1
to start after A.ps1
already terminated you'd need to create a scheduled task, though. Or add a delay at the beginning of B.ps1
:
Start-Sleep -Seconds 20
...