Categories

Advanced Functions part 2

I thought it would be worth while looking at the range of parameters we can use with our functions.  We saw the mandatory parameter last time but that is just a starter.  If we examine the range of parameters we see that there are some to control how we receive the parameters and a big bunch that perform validation on the values.


#Requires -version 2.0
function fname {
[CmdletBinding(SupportsShouldProcess
=$true/$false,
                     ConfirmImpact
="None"|"Low"|"Medium"|"High",
                     DefaultParameterSetName
="set_name")]
param (
    [parameter(Mandatory
=$true,
        Position
=0,
        ParameterSetName=”set_name”,
        ValueFromPipeline
=$true,
        ValueFromPipelineByPropertyName
=$true,
        HelpMessage
="Your message goes here")] 
    [alias("alias_name")]
    [datatype]
    [AllowNull()]
    [AllowEmptyString()]
    [AllowEmptyCollection()]
    [ValidateCount(
1,n)]
    [ValidateLength(
1,m)]
    [ValidatePattern(
"[A-Z][A-Z]A-Z][0-9]")]
    [ValidateRange(
0,10)]
    [ValidateScript({
$_ -lt 4})]
    [ValidateSet(
"red","blue","green")]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()] 
   $pname = default_value
)

    DynamicParam {PowerShell code}

  
Begin{}
  
Process{}
  
End{}

}


 


We wouldn’t actually use all of these at once on a single parameter – especially as some are mutually exclusive.


This list just covers the parameters – there are a bunch of methods we can use as well. We’ll look at those another time.


I’ll provide examples of using these in follow up posts


Leave a Reply