PowerShell Scripting and Modularization (TechMentor Fall 2011)


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • TechMentor Las Vegas 2011
  • TechMentor Las Vegas 2011
  • PowerShell Scripting and Modularization (TechMentor Fall 2011)

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