Scripting and Modularization in Windows PowerShell Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 4
Welcome! This is one of four sessions designed to teach specific Windows PowerShell skills Don Jones Windows PowerShell MVP Award Recipient PowerShell Columnist for  Microsoft TechNet Magazine “ Decision Maker” Columnist for  Redmond Magazine Author,  Learn Windows PowerShell in a Month of Lunches Co-Author,  Windows PowerShell v2.0: TFM Creator of numerous self-paced PowerShell training videos for CBTNuggets.com.
Agenda Scripting language overview (and where to find the docs) Modularization: Going from a command to your very own cmdlet-like tool that can be easily distributed to other administrators If we have time, we’ll even touch on error handling and add that to the example!
Caution: Demos Ahead! Please note:  This session is built primarily around demonstrations, and answering your questions. You won’t see many slides. If you’re looking for additional written resources, there are lots to choose from (including many free ones) – the closing slide will list some key URLs Reference slides in this deck contain reminders for key help topics and commands
Bonus I’ll also post any scripts, as well as these slides. Download location will be listed at the end of this session. Don’t bother copying down commands – you’ll be able to download the whole session!
The Master Plan Let’s create a distributable module It will contain a command that retrieves inventory information from one or more computer We’ll start by describing the ways in which we want the command to operate, and then build it to match We’ll wrap with a discussion on modularization design principles
Scripting Constructs PowerShell isn’t a scripting language… but it contains a small one We’ll look at three key constructs; there are others, but they’re less-frequently used If…ElseIf…Else ForEach Do…While
Let’s Start Building We’ll start in the console with a single command… … and move into a script editor to build upon it. Script editors you should know about: PowerGUI (powergui.org)    free edition available PowerShell Plus (idera.com) PrimalScript (sapien.com) PowerSE (devfarms.com)    free
Reference: Key Help Files Help about_*  Complete list of these and other topics About_if, About_ForEach, About_Select, etc for specific help with constructs About_Functions_Advanced_* for help on “script cmdlets” (Advanced Functions) About_Comment_Based_Help About_Modules
Reference: Script Modules Think of a module name… like “Fred.” \Documents \WindowsPowerShell \Modules \Fred Fred.psm1
Reference: Manifest Modules If your script module depends upon other modules, create a manifest using  New-ModuleManifest Specify your module name as the root module Specify your dependencies as  nested modules Name the manifest  modulename .psd1 (e.g., fred.psd1) Now, Import-Module will load your module  and  the ones it depends upon (if they’re not already loaded)
Design Principles One function, one task: Get something (e.g., computer names from a DB) Do something (operate against a bunch of something) Deal with output (format, export, etc) Functions should rarely include internal filtering, sorting, etc – output objects and let PowerShell’s native cmdlets do that Scripts shouldn’t be 900 lines that do 50 things – break each task down into a discrete function, and then call the functions
PowerShell Proverbs Every time someone writes a PowerShell command that outputs text instead of objects, God kills a puppy. Harsh, but true.  Output objects.  There are a variety of syntaxes for doing so – I’ve used the one that most people find readable and concise.
Thank You! Please feel free to hit me up with any remaining questions between sessions Please submit a session evaluation!  These are an extremely important part of ensuring that the conference continues to provide you with the education you need! Resources and URLs on the next slide…
Remember! Slides, and scripts will be posted within a few days. You’re welcome to download whatever you like; please don’t re-post anything elsewhere on the Internet. For the download URL, see my Twitter feed: @concentrateddon Twitter.com/concentrateddon
More PowerShell Resources Web Sites ShellHub.com Bit.ly/DonJones YouTube.com/ ConcentratedDon ITPro. ConcentratedTech.com Thank you again for attending! Available Here!

PowerShell Scripting and Modularization (TechMentor Fall 2011)

  • 1.
    Scripting and Modularizationin Windows PowerShell Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 4
  • 2.
    Welcome! This isone of four sessions designed to teach specific Windows PowerShell skills Don Jones Windows PowerShell MVP Award Recipient PowerShell Columnist for Microsoft TechNet Magazine “ Decision Maker” Columnist for Redmond Magazine Author, Learn Windows PowerShell in a Month of Lunches Co-Author, Windows PowerShell v2.0: TFM Creator of numerous self-paced PowerShell training videos for CBTNuggets.com.
  • 3.
    Agenda Scripting languageoverview (and where to find the docs) Modularization: Going from a command to your very own cmdlet-like tool that can be easily distributed to other administrators If we have time, we’ll even touch on error handling and add that to the example!
  • 4.
    Caution: Demos Ahead!Please note: This session is built primarily around demonstrations, and answering your questions. You won’t see many slides. If you’re looking for additional written resources, there are lots to choose from (including many free ones) – the closing slide will list some key URLs Reference slides in this deck contain reminders for key help topics and commands
  • 5.
    Bonus I’ll alsopost any scripts, as well as these slides. Download location will be listed at the end of this session. Don’t bother copying down commands – you’ll be able to download the whole session!
  • 6.
    The Master PlanLet’s create a distributable module It will contain a command that retrieves inventory information from one or more computer We’ll start by describing the ways in which we want the command to operate, and then build it to match We’ll wrap with a discussion on modularization design principles
  • 7.
    Scripting Constructs PowerShellisn’t a scripting language… but it contains a small one We’ll look at three key constructs; there are others, but they’re less-frequently used If…ElseIf…Else ForEach Do…While
  • 8.
    Let’s Start BuildingWe’ll start in the console with a single command… … and move into a script editor to build upon it. Script editors you should know about: PowerGUI (powergui.org)  free edition available PowerShell Plus (idera.com) PrimalScript (sapien.com) PowerSE (devfarms.com)  free
  • 9.
    Reference: Key HelpFiles Help about_* Complete list of these and other topics About_if, About_ForEach, About_Select, etc for specific help with constructs About_Functions_Advanced_* for help on “script cmdlets” (Advanced Functions) About_Comment_Based_Help About_Modules
  • 10.
    Reference: Script ModulesThink of a module name… like “Fred.” \Documents \WindowsPowerShell \Modules \Fred Fred.psm1
  • 11.
    Reference: Manifest ModulesIf your script module depends upon other modules, create a manifest using New-ModuleManifest Specify your module name as the root module Specify your dependencies as nested modules Name the manifest modulename .psd1 (e.g., fred.psd1) Now, Import-Module will load your module and the ones it depends upon (if they’re not already loaded)
  • 12.
    Design Principles Onefunction, one task: Get something (e.g., computer names from a DB) Do something (operate against a bunch of something) Deal with output (format, export, etc) Functions should rarely include internal filtering, sorting, etc – output objects and let PowerShell’s native cmdlets do that Scripts shouldn’t be 900 lines that do 50 things – break each task down into a discrete function, and then call the functions
  • 13.
    PowerShell Proverbs Everytime someone writes a PowerShell command that outputs text instead of objects, God kills a puppy. Harsh, but true. Output objects. There are a variety of syntaxes for doing so – I’ve used the one that most people find readable and concise.
  • 14.
    Thank You! Pleasefeel free to hit me up with any remaining questions between sessions Please submit a session evaluation! These are an extremely important part of ensuring that the conference continues to provide you with the education you need! Resources and URLs on the next slide…
  • 15.
    Remember! Slides, andscripts will be posted within a few days. You’re welcome to download whatever you like; please don’t re-post anything elsewhere on the Internet. For the download URL, see my Twitter feed: @concentrateddon Twitter.com/concentrateddon
  • 16.
    More PowerShell ResourcesWeb Sites ShellHub.com Bit.ly/DonJones YouTube.com/ ConcentratedDon ITPro. ConcentratedTech.com Thank you again for attending! Available Here!

Editor's Notes

  • #2 TechMentor Las Vegas 2011
  • #3 TechMentor Las Vegas 2011