Quantcast
Viewing all articles
Browse latest Browse all 122

PowerShell Multiple DynamicParam

I'm attempting to create a function for a script module that verifies the previous parameter has been set before it shows the next parameter.

The parameters I need are Identity, Share and Quota. I always want Identity to show, I don't want Share to show until the Identity has been set and I don't want Quota to show until the Share has been set.

I'm able to easily access $Identity, I'm not able to access $Share from within DynamicParam{}. I stepped through the script using PowerGUI and I was only able to see $Share when I hit Begin{}.

I have a way to workaround this by just showing Share/Quota if Identity is set, but ultimately I would like to learn how to keep adding additional parameters based on the previously set parameter.

A copy of the function is below. personfileutility.exe is just an executable we use to interact with the various systems to provision and gather information on a user.

function New-PersonalFiles{    [CmdletBinding()]    Param    (        # Param1 help description        [Parameter(Mandatory=$true)]        $Identity    )    DynamicParam        {            $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary            if (($Identity -notlike "") -or ($Identity -notlike $null)){                $attributes = new-object System.Management.Automation.ParameterAttribute                $attributes.ParameterSetName = "__AllParameterSets"                $attributes.Mandatory = $true                $lookup = [xml](\\servername\personalfiles\personfileutility.exe -a $Identity)                $type = $lookup.User.user_directory.type.type                if ($type -like "other" -or $type -like "staff") {                    $arguments = @()                    $arguments += "\\fileserver\sharename"                }                elseif ($type -like "faculty") {                    $arguments = @()                    $arguments += "\\fileserver\sharename"                    $arguments += "\\fileserver\sharename2"                }                elseif ($type -like "student") {                    $arguments = @()                    $arguments += "\\fileserver2\sharename"                }                $ParamOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $arguments                $attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]                $attributeCollection.Add($attributes)                $attributeCollection.Add($ParamOptions)                $dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Share", [String], $attributeCollection)                $paramDictionary.Add("Share", $dynParam1)            }            if (($Share -like "\\fileserver\*"))            {                $attributes2 = new-object System.Management.Automation.ParameterAttribute                $attributes2.ParameterSetName = "__AllParameterSets"                $attributes2.Mandatory = $true                $lookup = [xml](\\servername\personalfiles\personfileutility.exe -a $Identity)                $type = $lookup.User.user_directory.type.type                if ($type -like "other" -or $type -like "staff") {                    $arguments = @()                    $arguments += "15GB"                    $arguments += "20GB"                }                elseif ($type -like "faculty") {                    $arguments = @()                    $arguments += "10GB"                    $arguments += "15GB"                }                elseif ($type -like "student") {                    $arguments = @()                    $arguments += "5GB"                    $arguments += "10GB"                }                $ParamOptions2 = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $arguments2                $attributeCollection2 = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]                $attributeCollection2.Add($attributes2)                $attributeCollection2.Add($ParamOptions2)                $dynParam2 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Quota", [String], $attributeCollection2)                $paramDictionary.Add("Quota", $dynParam2)            }            return $paramDictionary        }<#    .Synopsis       Short description    .DESCRIPTION       Long description    .EXAMPLE       Example of how to use this cmdlet    .EXAMPLE       Another example of how to use this cmdlet    #>    Begin    {    }    Process    {        \\servername\personalfiles\personfileutility.exe -a $Identity -c -q ((Invoke-Expression $PSBoundParameters.Quota)  / 1KB) -s $Share    }    End    {    }}

Viewing all articles
Browse latest Browse all 122

Trending Articles