Resources/Samples/Function.ps1
Function Verb-Noun { <# .SYNOPSIS Brief summary of function .DESCRIPTION Function description .PARAMETER Parameter (Required) or not? Describe parameter usage .PARAMETER Parameter2 (Required) or not? Describe parameter usage .EXAMPLE Give examples of the function usage .NOTES Any other notes the user may need to know about the function .LINK Any links with additional information on the use of the function #> [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory=$true,Position=0, HelpMessage = "FirstParamater Message")] [string]$FirstParamater, [Parameter(Mandatory=$true,Position=1, HelpMessage = "Specify the action to perform: eg Create, Delete, or List.")] [ValidateSet("Create", "Delete", "List")] [string]$Action, [Parameter(Mandatory=$true,Position=1, HelpMessage = "SecondParameter Message")] [boolean]$SecondParameter ) begin { Write-Debug -Message "Begin '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" Write-Verbose -Message "FirstParamater '$($FirstParamater)'" Write-Verbose -Message "Action '$($Action)'" Write-Verbose -Message "SecondParameter '$($SecondParameter)'" if($FirstParamater -eq "SomethingBad"){ throw ":( something bad happened, please check how to use FirstParamater parameter and try again." } $result = @{} } process { try { Write-Information -Message "Parameter is '$($Parameter)'" Write-Information -Message "Parameter2 is '$($Parameter2)', if true write a warning, else write error and exit." if ($Parameter2) { Write-Warning -Message "Testing a Warning" } else { Write-Error -Message "Testing an Error" exit } if ($PSCmdlet.ShouldProcess("Target", "Action")) { # Your action here Write-Output $result } } catch { if ($_.Exception -and $_.Exception.Message) { Write-Error "An error occurred: $($_.Exception.Message)" } else { Write-Error "An error occurred, but no additional information is available." } } } end { if ($?) { Write-Debug -Message "End '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } } } |