PS : DynamicParam

How to use on DynamicParam in a function based upon input from a parameter

Cmdlet Dynamic Parameters

Cmdlets can define parameters that are available to the user under special conditions, such as when the argument of another parameter is a specific value. These parameters are added at runtime and are referred to as dynamic parameters because they are added only when they are needed. For example, you can design a cmdlet that adds several parameters only when a specific switch parameter is specified.

Get more info about these DynamicParam : https://msdn.microsoft.com/en-us/library/dd878299(v=vs.85).aspx

function get-info {

Param(
    [Parameter()]
    [ValidateSet('A', 'B')]
    [string] $Target
)

DynamicParam {
        # Ensure $Target is defined
        try { [void]$Target }
        catch { $Target = [string]::Empty }

        if ($Target -eq 'B') {
            $target += "...si si"
        }
    }
end {
        Write-Host $Target
    }
}

About the Author

wvsandwijk

I am a Technical Specialist - Cloud Infrastructures from the Netherlands. My Skills are mainly PowerShell, VMWare, Hyper-V, Microsoft System Center products and Windows Azure Pack. I work for a company in Utrecht The Netherlands.

No Comments

Comments are closed.