16
Scripting and Scripting and Modularization in Modularization in Windows PowerShell Windows PowerShell Don Jones Don Jones ConcentratedTech.com Learn Windows PowerShell in 2 Days / Part 4

PowerShell Scripting and Modularization (TechMentor Fall 2011)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Scripting and Scripting and Modularization in Modularization in Windows PowerShellWindows PowerShellDon JonesDon JonesConcentratedTech.com

Learn Windows PowerShell in 2 Days / Part 4

Page 2: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Welcome!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.

Page 3: PowerShell Scripting and Modularization (TechMentor Fall 2011)

AgendaAgenda

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!

Page 4: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Caution: Demos Ahead!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

Page 5: PowerShell Scripting and Modularization (TechMentor Fall 2011)

BonusBonus

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!

Page 6: PowerShell Scripting and Modularization (TechMentor Fall 2011)

The Master PlanThe 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

Page 7: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Scripting ConstructsScripting 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

Page 8: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Let’s Start BuildingLet’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

Page 9: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Reference: Key Help FilesReference: 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

Page 10: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Reference: Script ModulesReference: Script Modules

Think of a module name… like “Fred.”

• \Documents• \WindowsPowerShell

• \Modules• \Fred• Fred.psm1

Page 11: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Reference: Manifest ModulesReference: 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)

Page 12: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Design PrinciplesDesign 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

Page 13: PowerShell Scripting and Modularization (TechMentor Fall 2011)

PowerShell ProverbsPowerShell 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.

Page 14: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Thank You!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…

Page 15: PowerShell Scripting and Modularization (TechMentor Fall 2011)

Remember!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

Page 16: PowerShell Scripting and Modularization (TechMentor Fall 2011)

More PowerShell ResourcesMore PowerShell Resources

Web Sites ShellHub.com Bit.ly/DonJones YouTube.com/

ConcentratedDon ITPro.ConcentratedTech.

com

Thank you again for attending!

Available Here!