ps scripting and modularization
DESCRIPTION
TRANSCRIPT
Windows PowerShell Scripting & Windows PowerShell Scripting & ModularizationModularizationDon 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
AgendaAgenda
Start with a Command Move to a Script Parameterize the Script Encapsulate in a Function Using Dot-Sourcing More Complex: Pipeline Functions Adding Help Building a Script Module Making a “Script Cmdlet”
4 • Don Jones • ConcentratedTech.com
WARNING: FIREHOSE MODEWARNING: FIREHOSE MODE
I’m going to be moving quickly through some seriously complex stuff
Rely on the downloadable scripts as your reference – while you’re listening to me, the concepts are more important.
This is a lot to cover in 75 minutes. Consider this an introduction to capabilities – that you’ll explore in more detail on your own. Don’t try to memorize right now!
5 • Don Jones • ConcentratedTech.com
Start with a CommandStart with a Command
Get the command working in the shell, first.
Easier to debug – immediate feedback.
6 • Don Jones • ConcentratedTech.com
Move to a ScriptMove to a Script
Paste commands into a script. Done.
7 • Don Jones • ConcentratedTech.com
Parameterize the ScriptParameterize the Script
Add a PARAM() block to define parameters
Replace hardcoded, changeable information with parameters
Provide a data type ([string],[int], etc) and possibly a default
Don’t worry about making parameters “mandatory” or prompting at this point; we’ll evolve to that
8 • Don Jones • ConcentratedTech.com
Encapsulate in a FunctionEncapsulate in a Function
Move parameter definition to inside a function
Bonus: Don’t output text. Never use Write-Host. Instead, create custom objects – enabled far better reusability.
9 • Don Jones • ConcentratedTech.com
Using Dot-SourcingUsing Dot-Sourcing
. Path-to-Script Loads script into the current scope –
functions remain defined after script exits
Basically a way to “include” the functions from one script into another, or into the global shell
10 • Don Jones • ConcentratedTech.com
More Complex: Pipeline FunctionsMore Complex: Pipeline Functions
Includes BEGIN, PROCESS, and END script blocks
With PROCESS, pipeline objects are placed into $_ placeholder
Deal with one object at a time Hint: Add actual “functionality” in a
separate function… will make evolution easier
11 • Don Jones • ConcentratedTech.com
Adding HelpAdding Help
Help about_comment_based* Specially-formatted comments can be
parsed and presented as standardized help
No need to define a –help or -? parameter yourself!
12 • Don Jones • ConcentratedTech.com
Building a Script ModuleBuilding a Script Module
Rename .ps1 and .psm1 to make it a Script Module
Load using Import-Module – no need to dot-source
All functions in module are automatically exposed
You can also define aliases if desired Locate in
…/Documents/WindowsPowerShell/Modules/Module-name/module-name.psm1 for easier loading
13 • Don Jones • ConcentratedTech.com
Making a Making a ““Script CmdletScript Cmdlet””
Lets you formally specify parameters Parameters have attributes like
Mandatory, validation, etc. Parameters can bind pipeline input PROCESS script block executes once
for each object piped in; if multiple objects are given to a parameter WITHOUT using the pipeline, you’ll need to manually enumerate– Trick: Embed main functionality in a private
function
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 on 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