Download - From VB Script to PowerShell
Convert Your Brain from VBScript to Convert Your Brain from VBScript to PowerShellPowerShellDon JonesDon JonesSenior Partner & Principal TechnologistConcentrated Technology, LLC
This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it
within your own organization however you like.
For more information on our company, including information on private classes and upcoming conference appearances, please
visit our Web site, www.ConcentratedTech.com.
For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg
This work is copyright ©Concentrated Technology, LLC
CAUTION: DEMOS AHEADCAUTION: DEMOS AHEAD
This is a demonstration-intensive session (very few slides)
I will capture a shell transcript and save all of my scripts
You can download these (in a week or so) from ConcentratedTech.com (there’s a “Conference Materials” link in the menu)
3 • Don Jones • ConcentratedTech.com
Change Your BrainChange Your Brain
VBScript is a scripting language PowerShell is a shell that has some
scripting capabilities In many cases you can do a line-by-
line conversion from VBScript to PowerShell – but you’re not taking advantage of PowerShell’s strengths
We’re going to look at some specific scenarios and try to change your brain!
4 • Don Jones • ConcentratedTech.com
The LanguageThe Language
PowerShell’s “scripting language” is about 14 keywords
It provides most of the language constructs you know from VBScript
Let’s look at where to find the help on them, because we aren’t really going to focus on them a lot here
We will look at Switch because it’s so different from Select Case
5 • Don Jones • ConcentratedTech.com
Changing Your BrainChanging Your Brain
GOOD If & Switch – good for
adding conditional commands in complex processes
AVOID ForEach – if you’re
enumerating a collection, you’re likely doing it wrong
Loops of any kind – often not needed, and a sign of a “VBS” approach rather than a “PSH” approach
6 • Don Jones • ConcentratedTech.com
Example #1Example #1
Reading a list of computer names and connecting to them via WMI
We’ll look at a VBS-style approach, and then a more compact PowerShell-style approach
Example #2Example #2
Combining info from multiple sources into a single table
Again, VBS approach first – then a couple of PowerShell-style approaches
8 • Don Jones • ConcentratedTech.com
The Trick with PowerShellThe Trick with PowerShell
PowerShell commands intrinsically provide collection enumeration within the pipeline
Many PowerShell command parameters natively accept collections/arrays (how can you tell?) so you don’t have to enumerate over the collection/array yourself
(Parens) let you embed embed entire commands as input to another command’s parameters
Many commands can accept other commands as parameters
9 • Don Jones • ConcentratedTech.com
Stop Producing TextStop Producing Text
Never use Write-Host. Write-Output to the pipeline – and
write objects, not text Also:
– Write-Debug ($DebugPreference)– Write-Error– Write-Warning– Write-Verbose
Produce objects
10 • Don Jones • ConcentratedTech.com
Modularization, the PowerShell Modularization, the PowerShell WayWay
Every unit – script, function, whatever – should do one thing and either:– Produce NO output– Produce ONE consistent kind of output to
the pipeline
Utilize verb-noun naming convention Use consistent parameter names Create functions designed to work
within the pipeline (ideally, “script cmdlets” or Advanced Functions)
Add help using comments
11 • Don Jones • ConcentratedTech.com
Never Do ThisNever Do This
Produce a list… …output it to a text file… …and then read that back in and
parse it.
Text is only to be used as a final output – not an intermediary
Never try to parse the output of a table (e.g., Format-Table) – here’s why
12 • Don Jones • ConcentratedTech.com
Where are my functions?!?Where are my functions?!?
PowerShell doesn’t have intrinsic data-manipulation functions
Instead, look at methods of…– String objects– DateTime objects
Also look at the static methods of the [Math] class, e.g., [Math]::Abs(-5)
13 • Don Jones • ConcentratedTech.com
Any Questions? Any Questions? & Closing Thoughts& Closing Thoughts
Use the pipeline. The pipeline isn’t a feature of
PowerShell, it’s at the heart and soul of how the shell is structured
For a lot of tasks, a command sequence (“pipeline”) can be a lot shorter than a VBScript – and just as effective
Think reusability – output objects, since those have maximum re-use.
14 • Don Jones • ConcentratedTech.com
Final Notes…Final Notes…
Please be sure to submit a session evaluation form!
Download slides & materials from www.ConcentratedTech.com within one week!
Blog, URLs, and other information is also available at www.ConcentratedTech.com for your reference
More resources at www.ShellHub.com Thank you very much!
15 • Don Jones • ConcentratedTech.com
This slide deck was used in one of our many conference presentations. We hope you enjoy it, and invite you to use it
within your own organization however you like.
For more information on our company, including information on private classes and upcoming conference appearances, please
visit our Web site, www.ConcentratedTech.com.
For links to newly-posted decks, follow us on Twitter:@concentrateddon or @concentratdgreg
This work is copyright ©Concentrated Technology, LLC