Powershell

“Dear Diary

…I can hear the argument about to happen.  I’m slipping up.   In a fit of anger I burst out “It’s your GET-CHILDITEM that’s the CONST problem…”

Those words, uttering vbScript’s very language within the house, echoed through the walls.  This won’t be the last I hear of this.   Dear Powershell I am so sorry to have offended thee! *Sniff*

And so we delve further into vbScripting and get a little loopier today.  Remember last time we saw a For loop in vbScript, it wasn’t all that different from Powershell really.   Which means it isn’t that hard to do a direct tranlation of vbScript to Powershell of any legacy scripts you find.  In doing so, you may learn more about Powershell and how IT can interface with Windows thanks to the art of the GrandMasters of vbScript.

There are two other loops like Powershell has.  In Powershell we have a Do WHILE and Do UNTIL loop.   Well son of a gun, we’ve them in vbScript as well!

The Do WHILE loop executes a series of lines “WHILE” a condition is TRUE.  The DO UNTIL loop executes a series of instructions UNTIL it is TRUE.   Really honestly, they are almost identical and useful in different respective situations.

Here’s a basic Do While in vbScript

COUNTER=1

Do
   WScript.Echo COUNTER
   COUNTER = COUNTER + 1
Loop While COUNTER < 20

This is a very simple Do While in vbScript that will count up to 20 and display the output WHILE the variable ‘COUNTER’ is less than 20.  This same code in Powershell would look like this

$COUNTER=1

DO {
    $COUNTER
    $COUNTER=COUNTER + 1
} WHILE ( COUNTER –lt 20 )

Really only a few minor changes in the Syntax.  It’s still the same loop and works identically.   The other loop is the DO UNTIL which in vbScript would look like this.

COUNTER=1

Do
   WScript.Echo COUNTER
   COUNTER = COUNTER + 1
Loop UNTIL COUNTER = 20

In Powershell that same loop would appear like this

$COUNTER=1

DO {
    $COUNTER
    $COUNTER=COUNTER + 1
} UNTIL ( COUNTER –eq 20 )

There you go!  Done!  One important note though.  In vbScript the “UNTIL” and the “WHILE” statements can exist BEFORE or AFTER the CODE block.   It is very important to know this because when it exists BEFORE the codeblock in vbScript, the Codeblock ONLY executes if the Condition is True.   In the previous examples (in both cases) the Code will execute at LEAST once even if the condtion is already TRUE.  In both these cases the evaluation happens after.   This can be mimicked in Powershell with a simple “IF” statement although not as efficient.

So in THIS example

COUNTER=21

Do UNTIL COUNTER > 20
   WScript.Echo COUNTER
   COUNTER = COUNTER + 1
Loop

Will never execute because it happens BEFORE the code block.  A twin to this in Powershell would be

$COUNTER=1

IF ( $COUNTER –lt 20 ) {

$DO {
    $COUNTER
    $COUNTER=COUNTER + 1
} UNTIL ( COUNTER -gt 20 )

}

Now it’s not a typical scenario to encounter this but it IS very important to know the difference if you’re translating a vbScript to Powershell.  The Syntax is almost identical but knowing how the loop flows when you move the WHILE or UNTIL in vbScript is important.

We’ll end this loopiness with one final loop next time around.

And Remember, the Power of Shell is in YOU

Sean
The Energized Tech

Advertisements