• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
From VB Script to PowerShell
 

From VB Script to PowerShell

on

  • 1,081 views

 

Statistics

Views

Total Views
1,081
Views on SlideShare
1,081
Embed Views
0

Actions

Likes
0
Downloads
28
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

From VB Script to PowerShell From VB Script to PowerShell Presentation Transcript

  • Convert Your Brain from VBScript to PowerShell Don Jones Senior Partner & Principal Technologist Concentrated 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 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)
    • Don Jones • ConcentratedTech.com
  • Change 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!
    • Don Jones • ConcentratedTech.com
  • The 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
    • Don Jones • ConcentratedTech.com
  • Changing 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
    • Don Jones • ConcentratedTech.com
  • Example #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 #2
    • Combining info from multiple sources into a single table
    • Again, VBS approach first – then a couple of PowerShell-style approaches
    • Don Jones • ConcentratedTech.com
  • The 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
    • Don Jones • ConcentratedTech.com
  • Stop 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
    • Don Jones • ConcentratedTech.com
  • Modularization, the PowerShell Way
    • 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
    • Don Jones • ConcentratedTech.com
  • Never 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
    • Don Jones • ConcentratedTech.com
  • 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)
    • Don Jones • ConcentratedTech.com
  • Any Questions? & 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.
    • Don Jones • ConcentratedTech.com
  • 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!
    • 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