Powershell v2 Function Template
Powershell v2 has updated what you can do with creating functions over Powershell V1. The whole function experience has been updated to a cmdlet like feel with error handling, parameter validation, help creation and of course, tab completion! The following is a bare bones template that I've commented inline for easy reading!
Powershell v2 has updated what you can do with creating functions over Powershell V1. The whole function experience has been updated to a cmdlet like feel with error handling, parameter validation, help creation and of course, tab completion! The following is a bare bones template that I've commented inline for easy reading!
function new-template {
<#
.SYNOPSIS
Brief description of what the function does
.DESCRIPTION
A better description
.NOTES
Function Name : new-template
Author : Adam Stone
Requires : PowerShell V2
.LINK
http://adadmin.blogspot.com/
.EXAMPLE
Simple usage
PS C:\> new-template -args values
.EXAMPLE
Simple usage
PS C:\> new-template -args values values etc
.PARAMETER first
A description of the first parameter
.PARAMETER targetdomain
A description of the sedond parameter
#>
#parameter validataion
param (
#define the position of the parameter if required, specify if it is mandatory, and give a help message if anything is incorrect
[Parameter(Position=0, Mandatory=$true,HelpMessage="Enter the source Domain DNS name (eg domain.com) of the OU Structure")]
#add an alias to accept different names through the pipeline
[alias("other")]
#choose one of these if required
#make sure the option is not null or empty
#[validatenotnullorempty()]
#valadate the input against a regex pattern
#[validatepattern("regex")]
#valadate the input against defined set of attributes
#[validateset("one","two")]
#define type if necessary and what the default value will be if not manditory
[string] $first = "abcde"
,
#new parameter after the comma
[Parameter(Mandatory=$false)]
[alias("out")]
[string] $fileout = "none"
)
#the processes the function will complete
process {
#do stuff!
}
}#end function
Comments
Post a Comment