Task Scheduling and Powershell's Start-Job

十年热恋 提交于 2020-08-05 11:04:32

问题


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?

Thank you, SUT


回答1:


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


来源:https://stackoverflow.com/questions/41977362/task-scheduling-and-powershells-start-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!