PS – DynamicParam

Hello together,

in this post i want to show you a very rare function attribute. It is called dynamicparam and as the name says it adds some features to create parameters dynamically when the user writes the execution function:



function Test-DynamicParam 
{
    [CmdletBinding()]
    param( 
        [Parameter(ValueFromPipeline=$true)]
        $Count 
    )
    
    
    dynamicparam
    {
        $paramDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
        foreach ($Property in (1..$Count))
        {
            $attributes = New-Object System.Management.Automation.ParameterAttribute
            $attributes.ParameterSetName = '__AllParameterSets'
            $attributes.Mandatory = $false
            $attributeCollection = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)
            $Name = "DynamicParam_$Property"
            $dynParam = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($Name,
            [int32], $attributeCollection)
            $paramDictionary.Add($Name, $dynParam)
        }
            
        $paramDictionary
    }
    
    
    begin
    {}
    
    process
    {    "Entered: $Count"
    }
    
    end
    {}
}

Test-DynamicParam -Count 5 

In this example the user enters the function name and the parameter $count. As you can see in the dynamicparam block now additional parameters are created. If you enter for example the number 5 – 5 additional parameters show up and can be given to the function.

DynamicParam.png

So you can add or remove parameters depending on the parameters which the user entered which can be very powerful if you write very complex functions.

Give it a look

Greetings,

David

Advertisements

Client – Cleanup Java

JavaCleanup.png

Hello together,

today i want to show you a script of mine which removes Java from the computer. It makes no compromises and removes really everything of it. At least every relevant file and registry entry which i had found to that point.

But why? And why the hell so complicated?

Continue reading