Created
December 14, 2017 21:01
-
-
Save johannesprinz/8fa4526179c676f469e9b0b01846f572 to your computer and use it in GitHub Desktop.
Basic advanced function template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Function { | |
[OutputType([String])] | |
[CmdletBinding( | |
SupportsShouldProcess=$true, | |
ConfirmImpact="None" | |
)] | |
param ( | |
[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Name | |
) | |
Begin{ | |
$formats = @{ | |
"Begin" = "Begin {0}..."; | |
"Process" = "...processing {0}..."; | |
"End" = "...ending {0}"; | |
}; | |
Write-Verbose -Message ($formats.Begin -f $MyInvocation.MyCommand); | |
$date = Get-Date; | |
} Process { | |
Write-Verbose -Message ($formats.Process -f $MyInvocation.MyCommand); | |
if ($pscmdlet.ShouldProcess($Name)) { | |
return "Welcome to Hello World $Name on $date"; | |
} | |
} End { | |
Write-Verbose -Message ($formats.End -f $MyInvocation.MyCommand); | |
} | |
} | |
"Bob", "Mary" | Test-Function -WhatIf | |
"Bob", "Mary" | Test-Function -Confirm | |
"Bob", "Mary" | Test-Function -Verbose | |
Test-Function -Name "Bob" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment