Wednesday, February 24, 2010

Snippets for Comment Based Help and Advanced Function Parameters in Powershell v2

Been working quite a bit on the module for group policy, and almost have something I feel is worth sharing.  In the meantime, if you have a script editor that supports the concept of “snippets”, or saved re-usable bits of script code, you might find this little bit useful.  I’ve had a hard time remembering the syntax and all the options for some of the new cool stuff we can use in scripts and functions in Powershell v2, so I saved these little bits after scouring the output of Get-Help for comment based help and advanced functions for (hopefully) the last time, haha.


Comment Based Help:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
<#
.SYNOPSIS
  A brief description of the function or script. This keyword can be used
  only once in each topic.
 
.DESCRIPTION
  A detailed description of the function or script. This keyword can be
  used only once in each topic.
 
.PARAMETER
  The description of a parameter. You can include a Parameter keyword for
  each parameter in the function or script syntax.
 
.EXAMPLE
  A sample command that uses the function or script, optionally followed
  by sample output and a description. Repeat this keyword for each example.
 
.INPUTS
  The Microsoft .NET Framework types of objects that can be piped to the
  function or script. You can also include a description of the input
  objects.
 
.OUTPUTS
  The .NET Framework type of the objects that the cmdlet returns. You can
  also include a description of the returned objects.
 
.NOTES
  Additional information about the function or script.
 
.LINK
  The name of a related topic. Repeat this keyword for each related topic.
 
.COMPONENT
  The technology or feature that the function or script uses, or to which
  it is related. This content appears when the Get-Help command includes
  the Component parameter of Get-Help.
 
.ROLE
  The user role for the Help topic. This content appears when the Get-Help
  command includes the Role parameter of Get-Help.
 
.FUNCTIONALITY
  The intended use of the function. This content appears when the Get-Help
  command includes the Functionality parameter of Get-Help.
 
.FORWARDHELPTARGETNAME
  Redirects to the Help topic for the specified command. You can redirect
  users to any Help topic, including Help topics for a function, script,
  cmdlet, or provider.
 
.FORWARDHELPCATEGORY
  Specifies the Help category of the item in ForwardHelpTargetName.
  Valid values are Alias, Cmdlet, HelpFile, Function, Provider, General,
  FAQ, Glossary, ScriptCommand, ExternalScript, Filter, or All. Use this
  keyword to avoid conflicts when there are commands with the same name.
 
.REMOTEHELPRUNSPACE
  Specifies a session that contains the Help topic. Enter a variable that
  contains a PSSession. This keyword is used by the Export-PSSession
  cmdlet to find the Help topics for the exported commands.
 
.EXTERNALHELP
  Specifies the path to an XML-based Help file for the script or function.
 
  For more information about the cmdlet Help XML-based Help file format,
  see "How to Create Cmdlet Help" in the MSDN (Microsoft Developer Network)
  library at http://go.microsoft.com/fwlink/?LinkID=123415.
#>

Advanced Function Parameters:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
[parameter(
   #Mandatory=$false,
   #Position=0,
   #ParameterSetName="SetName",
   #ValueFromPipeline=$false,
   #ValueFromPipelineByPropertyName=$false,
   #ValueFromRemainingArguments=$false,
   #HelpMessage="Help goes here"
)]
#[Alias("Alias"]
#[AllowNull()]
#[AllowEmptyString()]
#[AllowEmptyCollection()]
#[ValidateCount(min,max)]
#[ValidateLength(min,max)]
#[ValidatePattern("RegEx Goes Here")]
#[ValidateRange(min,max)]
#[ValidateScript({ $_ -eq "something" })]
#[ValidateSet("One","Two","Three")]
#[ValidateNotNull()]
#[ValidateNotNullOrEmpty()]
[System.Object]
$parameterName = "DefaultValue"

Hope that's helpful!