Powershell – That torrid vbScript affair – Part 7

Powershell

“…I stare about me.  OBJECTS are piped all over the place.  The VERB-NOUN resounded in my head.  (GET-PROCESS CSCRIPT | REMOVE-OBJECT) –or (GET-PROCESS YOU | REMOVE-OBJECT).  The words were clear.  End this Torrid affair with vbScript or find myself playing the BLUES as Powershell shunned me forever… There was only one choice.  A Final GET-FLING with vbScript and then … goodbyte…”

So we’re going to touch on some minor pieces of Syntax to get this out of our system.  No you won’t be an expert in vBscript from reading this.  But hopefully you’ll have just enough to muddle through the thousands of scripts to glean some useful knowledge.

I will point one of the BIGGEST thing that tripped me on vbScript.

This stupid little “-”

You see it all over the place in vbScript and it’s just MADDENING until you realize all it is.  A placeholder.  A little piece that says “Hey this line is too darn big to fit on one screen, so we’re going to break it up”

Powershell has the same feature.  Only in Powershell we use it all the time.  For us it’s the Backtick

—-> `

Yes, that “Backward’s single quote thingy”

But knowing that that is all there job helps’ us read the vbScript and make it more legible.  I agree, breaking the lines up is a good idea.  But sometimes when you’re translating it needs to be in a single line for it to make sense to us.

At least it does to me.

So looking at this little batch of nightmare from last time….

strComputer = "."
Set objWMIService = GetObject("winmgmts:" –
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

Set colItems = objWMIService.ExecQuery –
    ("Select * from Win32-LogicalDisk")

Read’s actually like THIS if you remove those little “-” Underscores breaking up the lines.

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

Set colItems = objWMIService.ExecQuery ("Select * from Win32-LogicalDisk")

Another thing that drove me bananas in vbScript was all of the “&” characters everything.   Never could make sense of them.  Turns out they’re not so bad either.  That Ampersand character is just another way of saying “And this”.

So a thing like this in vbScript that reads

This=”Something”
That=”Another thing”
OtherThing=”Something else completely Pointless”

wscript.echo This & “  “ & That & “—“ & Otherthing

Really is just this in Powershell

$This=”Something”
$That=”Another thing”
$OtherThing=”Something else completely Pointless”

Write-Host “$This  $That—$Otherthing”

Really it’s not a huge mystery language.   Just a slight difference in Syntax.  Once you unlock the little pieces, it’s actually pretty easy to redo vbScript into Powershell.

And now, if you all with pardon me, I need to create a “SEND-FLOWERS” function along with some “GET-FORGIVENESS” piped into.  I’m certain Powershell will forgive me this transgression… Someday.   If others learn because of this.  It will be worth it.

I will gladly suffer the punishment then.

Just remember

The Power of Shell is in YOU

Sean
The Energized Tech

Leave a Reply