PowerShell Functions
-
Upload
mikepfeiffer -
Category
Documents
-
view
798 -
download
2
description
Transcript of PowerShell Functions
PowerShellFunctions
Presented byMike Pfeiffer
http://www.AZPoSH.com
AgendaPowerShell Functions
What is a PowerShell Function?Structure of a PowerShell FunctionFunction GuidelinesInput ParametersOutputScopeFunctions in the PipelineBegin, Process and End BlocksParameter AttributesComment Based Help
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
Structure of a PowerShell Function
Function GuidelinesMust be defined before they are calledCall functions without parenthesis or
commas:Get-MyStuff –Param1 $value1 –Param2 $value2Get-MyStuff $value1 $value2
Input ParametersThe $args variableNamed and positional parametersParameter type constraintsInitializing parameters with default valuesThe switch parameter
OutputWrite-Host is for writing to the consoleUse Write-Output to return function outputDon’t use the Return keyword to return
output
ScopeFunctions have internal scopeDon’t use or modify variables from parent
scopeConsider using default values for internal
variables
Functions in the PipelineThe $input variableFilters
Begin, Process and End Blocks
Parameter Attributes
Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34
Comment Based Help
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
Thank You!