Skip to content

Instantly share code, notes, and snippets.

@johannesprinz
Created December 14, 2017 21:01
Show Gist options
  • Save johannesprinz/8fa4526179c676f469e9b0b01846f572 to your computer and use it in GitHub Desktop.
Save johannesprinz/8fa4526179c676f469e9b0b01846f572 to your computer and use it in GitHub Desktop.
Basic advanced function template
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