任务调度和Powershell的开始工作

时间:2017-02-01 10:28:33

标签: powershell cron scheduled-tasks taskscheduler start-job

目前,我尝试运行PowerShell脚本,该脚本以Start-Job -name $jobname -Scriptblock { ... Code ...};启动一些实例

此PowerShell脚本应在WIndows任务计划中每15分钟执行一次。我以最高权限运行此过程,Power-Shell-Script完美启动 - 问题是: 由Start-Job执行的代码不起作用。

我认为问题在于“Start-Job”无法与任务计划一起使用。

你知道如何解决这个问题吗?是否有任何设置要配置?

谢谢,SUT

1 个答案:

答案 0 :(得分:0)

如果脚本末尾没有任何控件,Powershell将在创建后台作业后立即退出。因此,请尝试添加以下控制代码:

$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
相关问题