Getting selected value of a parameter to provide values for next parameter in Powershell

204 Views Asked by At

I am writing a function where I am planning to provide dynamic parameter auto-completion. Idea is this;

When I type

Activate -User <USERNAME> (-User gets auto-completed by GetUsers class)

I wanna use <USERNAME> selected by -User parameter in GetValidValues method of GetProfiles class so I can dynamically determine what user directory I should be searching profile files in, since every user would have different profile files path.

I haven't been able to find a mechanism where I would get any selected value of any parameters. I am using PS6 and I was wondering if there is a way to do this. Here is my minimal code.

class GetUsers : System.Management.Automation.IValidateSetValuesGenerator
{
    [String[]] GetValidValues()
    {
        # I simply return the user array here, no big deal
        return [string[]] $usersArray
    }
}
class GetProfiles : System.Management.Automation.IValidateSetValuesGenerator
{
    [String[]] GetValidValues()
    {
        # Here I need to get what value is selected for $User parameter
        # of the function below, so I can populate $profileArray for selected
        # user
        return [string[]] $profileArray
    }
}
function global:Activate()
{
    [CmdletBinding()]
    Param
    (
        [ValidateSet([GetUsers])]
        [string]$User="",
        [ValidateSet([GetProfiles])]
        [string]$Profile="",
    )
    # do business as usual
    Write-Host $User
    Write-Host $Profile
}

Thanks!

0

There are 0 best solutions below