SUT
SUT

Reputation: 323

Task Scheduling and Powershell's Start-Job

Currently, I try to run a PowerShell-Script, which starts a few instances with Start-Job -name $jobname -Scriptblock { ... Code ...};

This PowerShell-Script should be executed every 15 minutes in the WIndows Task Schedule. I run this process with highest priviledges, and the Power-Shell-Script starts perfectly - the problem is: The Code, which is executed by Start-Job doesn't work.

I think the problem is that the "Start-Job" can not work with the task schedule together.

Do you know how to solve that? Are there any settings to configure?

Upvotes: 1

Views: 2677

Answers (1)

Tonkon
Tonkon

Reputation: 41

If no any control at the end of script, Powershell will immediately exit once background job is created. So try to add below control code:

$runningJobs = Get-Job -State Running 
    while($runningJobs.Count -gt 0){
    Start-Sleep -Seconds 1
    $runningJobs = Get-Job -State Running
}

Write-Host "Done!" -ForegroundColor Red -BackgroundColor Black

Upvotes: 2

Related Questions