Pages

Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

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.

Monday, December 9, 2013

Looping, making your scripts useful

Ah, looping. You either think this is the easiest thing on earth or, you told your boss something like this about 20 minutes ago "Yeah, we can get a script to do that, like, maybe through a list or something." Luckily creating loops in Powershell is very simple, with a few ways you can do it, but they all essentially do the same thing. Which is stuff. Repeatedley. If you read any of my previous articles, you might have noticed the one about Do and While loops. This is actually a loop as well, though not necessarily in the typical sense that most people use them for.

I'm feeling less verbose than usual today, so let's get this party started.

Pancake:
foreach ($item in $var)
    {
    #Process stuff
    }

So there we go. That's your first loop. For most people, you're going to want to be reading stuff from a list and performing some tasks based off of that. So if we wanted to grab stuff from a .txt file, then we could make a script similiar to the one below.

$ComputerList = Get-Content "C:\Working\computerlist.txt"

foreach ($Computer in $ComputerList)
    {
    Write-Host $Computer
    }

So what happened here? First we used Get-Content. Get-Content is used for many things, but usually it is for opening the content of a file. In this case we are using a list of computers. When you create a list in a text file, each line is a new object, for as many lines as there are. Do note if you have empty lines entered (let's say perhaps with nothing but spaces) then you will get some garbage in your script. As far as Get-Content is concerned, those white spaces are indeed content.

Once we got the content of that text file inside of our $ComputerList variable, we created a variable foreach item in that list. Starting from top to bottom, one line at a time. We can name this variable whatever we want, in my example, we named it $Computer. Because that just makes sense. You can name it $Pancakes (preferred), $Pretzels, whatever you want really. After that we open a scriptblock { }, and inside of that script block we can do whatever our hearts desire. You can nest as many foreach loops as you can logically stand (I get really confused after I start heavy-nesting), use if/else statements, anything you want essentially. Just think of it as another little script, except only for that item that you specified. It will run through the scriptblock, and then do it again for the next item in your list, until your list is exhausted. Once your list is exhausted, it will exit the loop and continue processing the rest of the script, if you have anything left to process, that is.

This is it, a short simple sweet (no pun intended) article. Later I will talk about other uses foreach loops, including foreach-object, reading from an array, building arrays, and so much more fun stuff!