14
PowerShell Functions Presented by Mike Pfeiffer http:// www.AZPoSH.com

PowerShell Functions

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: PowerShell Functions

PowerShellFunctions

Presented byMike Pfeiffer

http://www.AZPoSH.com

Page 2: PowerShell Functions

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

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

Structure of a PowerShell Function

Page 5: PowerShell Functions

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

Input ParametersThe $args variableNamed and positional parametersParameter type constraintsInitializing parameters with default valuesThe switch parameter

Page 7: PowerShell Functions

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

ScopeFunctions have internal scopeDon’t use or modify variables from parent

scopeConsider using default values for internal

variables

Page 9: PowerShell Functions

Functions in the PipelineThe $input variableFilters

Page 10: PowerShell Functions

Begin, Process and End Blocks

Page 11: PowerShell Functions

Parameter Attributes

Parameter Attribute Declaration (MSDN): http://bit.ly/bzGu34

Page 12: PowerShell Functions

Comment Based Help

Page 13: PowerShell Functions

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

Thank You!