Powershell

"Today’s article in Powershell is brought to you by the letter ‘P’ with Commentary from the “#” character.

This seems so lame to talk about but I think one of the simpler things to understand in Powershell was the simple concept that any line preceded by a “#” was just a comment, Words left by the Author.   Knowing that made it easier to read scripts and ignore certain information. 

vbScript has comments too.  No shock on that one.  They are similar to Powershell comments, ending with a hard return; but beginning with a lowly quote like this

 

Why do you even care what a Comment is? 

If you’re new to coding and scripting it is sometimes those little nuggets the author left behind to tell just WHAT was going on in their head.  Comments are sometimes that tiny FAQ sitting inside the script.   Instructions.

Understanding what a comment looks like ALSO helps simplify translation.  I say this because often what I have found is going from vbScript to Powershell that some code is beautifully commented.   So much so, that I can’t understand what is actually going on.  

As important as they are, it’s also equally as important sometimes to remove the verbal static that IS comments from the flow of a script for me to understand it.

Here is an example

————— Script End ————————-

‘ Sample script designed by Sean Kearney to demonstrate
’ TOO many comments in a vbScript to confuse
’ poor Powershell noobs and give headaches to the
’ the many people reading this blog
’ as each one of these lines contains a comment, sometimes TOO many
’ comments and placed in the Wrong spot make it difficult to read
on error resume Next
‘ the code which is not what a comment should do.  Oh shouldn’t I have
’ mentioned that the previous line was to control errors?  Oh nuts.  This
’ is why I don’t code for a living.   There is a constant coming up here
HelloMessage = "Hello I am a message"
‘ There it was!  Wow! what a lame thing to do, throwing a constant in
’ the way of my flow of typing.   I’m feeling a bit loopy now.  OH speaking of
‘ loops.  I think I was going to execute a loop for some reason.  Of course there
’ was a reason I was doing all of this
For A = 1 To 20
’ Oh yes.  Right.  I was going to show the readers not only an example of bad
’ Commenting but also how sometimes the code get’s lost in the comments
    WScript.Echo A & HelloMessage
’ And as a result it CAN be hard to understand what the program was doing
‘ so that we could translate it to Powershell
Next

————— Script End ————————-

Ok I’ll admin this is a HORRIBLE script with even worse comments, a trained Gerbil could have done better.  But it is only to serve one purpose.   For you to see how to see what a comment IS in vbScript and mostly to have a little fun.

The same script without all the jibbierish is

————— Script End ————————-

on error resume Next
HelloMessage = "Hello I am a message"
for A = 1 to 20
   wscript.echo A & HelloMessage
Next

————— Script End ————————-

What purpose did this serve? 

Just a little more understanding of the Syntax that is vbScript if you need to translate to Powershell.   And possibly a way to understand how to filter out that “extra stuff” when you’re just trying to see the flow of the logic.

Next time we’re going to go loopy.  Yes.  Loops in vbScript vs. Powershell.

Sean
the Energized Tech

Advertisements