我有一个脚本,用于启动作业和管理它们。我过去曾经成功使用过它,但是最近一个foreach循环将挂起。该脚本显示其进度以及已创建的线程(作业)数。脚本挂起后,我可以在任务管理器(进程浏览器)中看到正在运行的任务。当我结束在脚本下创建的最新Powershell任务时,该脚本将继续。我尝试使用多个作业脚本,但得到的结果相同。我从来没有失败过,所以没有任何日志可看。
它挂在“开始作业”上。根据我运行的子脚本,创建的作业总数多达1600个,少则60个,并且它会以随机迭代的形式挂起,直到终止该进程为止。挂。
$ScriptFile = $(".\DesMime2.ps1")
$MaxThreads = 20
$SleepTimer = 100
$MaxWaitAtEnd = 600
$filename = ".\Install3.csv"
$computers = (get-adcomputer -filter *).name
$Complete = Get-date
"Killing existing jobs . . ."
Get-Job | Remove-Job -Force
"Done."
clear-host
$i = 0
ForEach ($Computer in $Computers){
While ($(Get-Job -state running).count -ge $MaxThreads){
Write-Progress -Activity "Creating Server List" `
-Status "Waiting for threads to close" `
-CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" `
-PercentComplete ($i / $Computers.count * 100)
Start-Sleep -Milliseconds $SleepTimer
}
$i++
Start-Job -FilePath $ScriptFile -ArgumentList $Computer -Name $Computer #| Out-Null
Write-Progress -Activity "Server List" `
-Status "Starting Threads" `
-CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" `
-PercentComplete ($i / $Computers.count * 100)
}
While ($(Get-Job -State Running).count -gt 0){
$ComputersStillRunning = ""
ForEach ($System in $(Get-Job -state running)){$ComputersStillRunning += ", $($System.name)"}
$ComputersStillRunning = $ComputersStillRunning.Substring(2)
Write-Progress -Activity "List" `
-Status "$($(Get-Job -State Running).count) threads remaining" `
-CurrentOperation "$ComputersStillRunning" `
-PercentComplete ($(Get-Job -State Completed).count / $(Get-Job).count * 100)
If ($(New-TimeSpan $Complete $(Get-Date)).totalseconds -ge $MaxWaitAtEnd){"Killing all jobs still running . . .";Get-Job -State Running | Remove-Job -Force}
Start-Sleep -Milliseconds $SleepTimer
}
"Reading all jobs"
Get-Job | Receive-Job | Select-Object * -ExcludeProperty RunspaceId,PSComputerName,PSShowComputerName | export-csv $filename -notype