Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo

1

Windows PowerShell Scripting & Modularization Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC

2

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

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

16

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

More Related Content

PS scripting and modularization

  • 1. Windows PowerShell Scripting & Modularization Don Jones Senior Partner & Principal Technologist Concentrated Technology, LLC
  • 2. 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
  • 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
  • 16. 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

Editor's Notes

  1. MGB 2003 © 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.