Freigeben über


Scripting Tips & Tricks: Dynamic Parameters

Imagine you want a secondary parameter (or set of parameters) to ONLY appear if a particular value has been supplied to an initial parameter...

Eh?

Ok, so you have Parameter A. You supply Value A to Parameter A and you now, and only now, have access to Parameter B. Furthermore, if you were to supply Value B to Parameter A (rather than Value A) this exposes access to Parameter C.

This dynamic exposure of parameters, based on supplied parameter values, is defined via the DynamicParam language element.

The below function defines a parameter called -Mode. This parameter can only accept two values: "Report" and "Deactivate".

If the Report value is supplied then access to the -ReportPath parameter becomes available.

Likewise, if the Deactivate value is supplied then access to the -TargetOU and -LogPath parameters becomes available.

 

 
function Test-DynamicParam
{
    [CmdletBinding()]
    Param
    (
        #Function mode
        [Parameter(Mandatory=$true,Position=0)]
        [ValidateSet("Report","Deactivate")]
        [string]
        $Mode
    )

    DynamicParam {


        switch ($Mode) {


            "Report" {

                $Attributes = New-Object -TypeName System.Management.Automation.ParameterAttribute
                $Attributes.Mandatory = $true

                $AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
                $AttributeCollection.Add($Attributes)

                $ReportPath = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("ReportPath",[string],$AttributeCollection)

                $ParamDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
                $ParamDictionary.Add("ReportPath",$ReportPath)

                return $ParamDictionary
                break

            }


            "Deactivate" {

                $Attributes = New-Object -TypeName System.Management.Automation.ParameterAttribute
                $Attributes.Mandatory = $true

                $AttributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
                $AttributeCollection.Add($Attributes)

                $TargetOu = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("TargetOu",[string],$AttributeCollection)
                $LogPath = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("LogPath",[string],$AttributeCollection)

                $ParamDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
                $ParamDictionary.Add("TargetOu",$TargetOu)
                $ParamDictionary.Add("LogPath",$LogPath)

                return $ParamDictionary
                break

            }


        }

    }

}

 

Running the function...

 

Notice how the ReportPath, TargetOu and LogPath parameters only become available under specific conditions.