PowerShell Functions

1,056 views

Published on

Presentation on PowerShell Functions for the Arizona PowerShell User Group June Meeting.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,056
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PowerShell Functions

  1. 1. PowerShell Functions Presented by Mike Pfeiffer http://www.AZPoSH.com
  2. 2. Agenda <ul><li>PowerShell Functions </li></ul><ul><ul><li>What is a PowerShell Function? </li></ul></ul><ul><ul><li>Structure of a PowerShell Function </li></ul></ul><ul><ul><li>Function Guidelines </li></ul></ul><ul><ul><li>Input Parameters </li></ul></ul><ul><ul><li>Output </li></ul></ul><ul><ul><li>Scope </li></ul></ul><ul><ul><li>Functions in the Pipeline </li></ul></ul><ul><ul><li>Begin, Process and End Blocks </li></ul></ul><ul><ul><li>Parameter Attributes </li></ul></ul><ul><ul><li>Comment Based Help </li></ul></ul>
  3. 3. What is a PowerShell Function? <ul><li>Think of it as a block of code that behaves as a cmdlet </li></ul><ul><li>Can be loaded from your profile, dot sourced via a script, or imported from a module </li></ul><ul><li>Can be called or defined in another function or script </li></ul>
  4. 4. Structure of a PowerShell Function
  5. 5. Function Guidelines <ul><li>Must be defined before they are called </li></ul><ul><li>Call functions without parenthesis or commas: </li></ul><ul><ul><li>Get-MyStuff –Param1 $value1 –Param2 $value2 </li></ul></ul><ul><ul><li>Get-MyStuff $value1 $value2 </li></ul></ul>
  6. 6. Input Parameters <ul><li>The $args variable </li></ul><ul><li>Named and positional parameters </li></ul><ul><li>Parameter type constraints </li></ul><ul><li>Initializing parameters with default values </li></ul><ul><li>The switch parameter </li></ul>
  7. 7. Output <ul><li>Write-Host is for writing to the console </li></ul><ul><li>Use Write-Output to return function output </li></ul><ul><li>Don’t use the Return keyword to return output </li></ul>
  8. 8. Scope <ul><li>Functions have internal scope </li></ul><ul><li>Don’t use or modify variables from parent scope </li></ul><ul><li>Consider using default values for internal variables </li></ul>
  9. 9. Functions in the Pipeline <ul><li>The $input variable </li></ul><ul><li>Filters </li></ul>
  10. 10. Begin, Process and End Blocks
  11. 11. Parameter Attributes Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34
  12. 12. Comment Based Help
  13. 13. Resources <ul><li>Get-Help About_Functions </li></ul><ul><li>Get-Help About_Functions_Advanced </li></ul><ul><li>Get-Help About_Comment_Based_Help </li></ul><ul><li>PowerShell in Action </li></ul><ul><ul><li>http://manning.com/payette2/ </li></ul></ul><ul><li>PowerGUI PowerShell v2 Snippets </li></ul><ul><ul><li>http://bit.ly/azDkJG </li></ul></ul>
  14. 14. Thank You!

×