0

I use this script to run some jobs:

#========================================================================
#Get User stats with ADInfo < --- need to see if this can be converted to native PowerShell
$XMLConfig = Get-ChildItem c:\ADInfo\XML_Config_Files

$Jobs = @()

#loop through each config file and run ADAudit - there is one file per domain
foreach ($config in $XMLConfig) {
        write-host "Starting a background job for $($config.name)"
        $Jobs += start-job -ScriptBlock {c:\ADInfoCmd.exe /config $args[0] } -ArgumentList $config.fullname.tostring()
}

write-host "`nJobs are running"


#=======================================================================
#End of script 

Some jobs take much longer than others and I would like to be able to send a user friendly update to the console when any one of the started jobs are still running to show the script hasn't stalled.

I tried something like this

do{
write-host "working..."
}
while (wait-job $jobs)

but it writes once and then waits for the jobs to finish

I tried this

$joblist = get-job $jobs | where state -eq running
while ($joblist){
write-host "working..."
}

but I get an error for all the jobs get-job : The command cannot find the job because the job name System.Management.Automation.PSRemotingJob was not found and $joblist is never assigned a value.

Is there a way to do this?

sailingbikeruk
  • 164
  • 1
  • 8
  • 1
    Found a nice article [PowerShell – How to Display Job Progress](https://key2consulting.com/powershell-how-to-display-job-progress/). Perhaps this is what you want? – Theo Jan 21 '22 at 12:23
  • Here you have an example using `Write-Progress` https://stackoverflow.com/questions/70705081/how-to-automatically-update-the-count-of-background-jobs-running-in-powershell/70705153#70705153 and here another example using `Format-Table` https://stackoverflow.com/questions/70705548/get-job-format-table-with-runtimes-of-jobs/70705634#70705634 – Santiago Squarzon Jan 21 '22 at 13:12

1 Answers1

1

I had passed the entire PS Object to get-job. It worked when I passed only the job ID

This is what I ended up using and provides enough feedback to the user to demonstrate the script is still working.

write-host "`nJobs are running" -ForegroundColor Yellow -NoNewline
$RunningJobs = Get-Job $jobs.id | where state -eq running
while($runningjobs){
    write-host "." -NoNewline
    $RunningJobs = Get-Job $jobs.id | where state -eq running
    Start-Sleep -Seconds 3
}
Write-host "Background Jobs Complete"
Write-Host "Script Ends" -ForegroundColor Yellow
sailingbikeruk
  • 164
  • 1
  • 8
  • It would be more explicit and quickly understood to use `while($null -ne $runningjobs)`. – lit Jan 21 '22 at 16:19