powershell functions
DESCRIPTION
Presentation on PowerShell Functions for the Arizona PowerShell User Group June Meeting.TRANSCRIPT
![Page 1: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/1.jpg)
PowerShellFunctions
Presented byMike Pfeiffer
http://www.AZPoSH.com
![Page 2: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/2.jpg)
AgendaPowerShell Functions
What is a PowerShell Function?Structure of a PowerShell FunctionFunction GuidelinesInput ParametersOutputScopeFunctions in the PipelineBegin, Process and End BlocksParameter AttributesComment Based Help
![Page 3: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/3.jpg)
What is a PowerShell Function?Think of it as a block of code that behaves as
a cmdletCan be loaded from your profile, dot sourced
via a script, or imported from a moduleCan be called or defined in another function
or script
![Page 4: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/4.jpg)
Structure of a PowerShell Function
![Page 5: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/5.jpg)
Function GuidelinesMust be defined before they are calledCall functions without parenthesis or
commas:Get-MyStuff –Param1 $value1 –Param2 $value2Get-MyStuff $value1 $value2
![Page 6: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/6.jpg)
Input ParametersThe $args variableNamed and positional parametersParameter type constraintsInitializing parameters with default valuesThe switch parameter
![Page 7: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/7.jpg)
OutputWrite-Host is for writing to the consoleUse Write-Output to return function outputDon’t use the Return keyword to return
output
![Page 8: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/8.jpg)
ScopeFunctions have internal scopeDon’t use or modify variables from parent
scopeConsider using default values for internal
variables
![Page 9: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/9.jpg)
Functions in the PipelineThe $input variableFilters
![Page 10: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/10.jpg)
Begin, Process and End Blocks
![Page 11: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/11.jpg)
Parameter Attributes
Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34
![Page 12: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/12.jpg)
Comment Based Help
![Page 13: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/13.jpg)
ResourcesGet-Help About_FunctionsGet-Help About_Functions_AdvancedGet-Help About_Comment_Based_HelpPowerShell in Action
http://manning.com/payette2/PowerGUI PowerShell v2 Snippets
http://bit.ly/azDkJG
![Page 14: PowerShell Functions](https://reader033.vdocument.in/reader033/viewer/2022061613/5560b86fd8b42aef3b8b4b59/html5/thumbnails/14.jpg)
Thank You!