Pages

Saturday, February 15, 2014

Some useful notes

I've come accross a few oddities recently on jobs, passing variables to them, along with Invoke-Expression. As well as a new tip on throttling your jobs.

I was unsatisfied that my jobs were being removed as a form of throttle control. If we recall from an earlier blog post I had done here, we were looking at the following bit of code to control how many jobs we were running at the same time;

DO { 
Remove-Job -State Completed
} until (@(Get-Job).Count -le 25)

This of course works, however if you are running 50 jobs, when you do Get-Job after they have all kicked off, you're going to be left with information on only 25 jobs. Fortunately, this is as easy to fix as a hot pocket. All we need to do is change it to the following;

DO { 
Start-Sleep -Seconds 1
} until (@(Get-Job -State Running).Count -le 25)

This lets us halt the creation of new jobs as long as the running job count is 25. This is a much better solution, as we can still do Receive-Job on all of our prior jobs, and none of them get removed from our console.

Another oddity I came across was variables taking the place of other variables in -ArgumentList. There is no easy way to explain it, however I noticed my variables in my jobs were using other variables, or submitting a null value. After using Write-Host way too many times, I found that some of my variables were not being passed correctly to my job, or one variable, was showing the value of another variable! How odd is that? It came down to passing variables that didn't exist in the job, or missing a variable that you have forgotten to pass. In this case things get all screwey, and your variables are either blank, or using values from other variables, depending on their placement in -ArgumentList. If in doubt, using Write-Host and then using Receive-Job to see what values your variables have, is a good way to troubleshoot and issue like this.

I found this quite odd and this is the first time I've run into it.

No comments: