This document is a slide deck presentation about Windows PowerShell scripting and modularization. The presentation covers topics such as starting with commands, moving to scripts, parameterizing scripts, encapsulating in functions, using dot-sourcing, building pipeline functions, adding help, building script modules, and making script cmdlets. The presentation provides examples and guidance for improving PowerShell scripts through modularization and best practices.
1 of 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
More Related Content
PS scripting and modularization
1. Windows PowerShell Scripting & Modularization Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC
3. 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
4. Agenda 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” • Don Jones • ConcentratedTech.com
5. WARNING: 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! • Don Jones • ConcentratedTech.com
6. Start with a Command Get the command working in the shell, first. Easier to debug – immediate feedback. • Don Jones • ConcentratedTech.com
7. Move to a Script Paste commands into a script. Done. • Don Jones • ConcentratedTech.com
8. Parameterize 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 • Don Jones • ConcentratedTech.com
9. Encapsulate 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. • Don Jones • ConcentratedTech.com
10. Using 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 • Don Jones • ConcentratedTech.com
11. More 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 • Don Jones • ConcentratedTech.com
12. Adding 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! • Don Jones • ConcentratedTech.com
13. Building 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 • Don Jones • ConcentratedTech.com
14. Making a “Script 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 • Don Jones • ConcentratedTech.com
15. 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! • Don Jones • ConcentratedTech.com