1

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!

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
verte
  • 49
  • 1
  • 2
  • Are you familiar [with `ArgumentCompleter`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-6) or [its associated attribute](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.argumentcompleterattribute?view=powershellsdk-1.1.0)? – Maximilian Burszley Mar 26 '19 at 16:07
  • 1
    PSv6 is powershell-core, you should switch to that tag. –  Mar 26 '19 at 16:23
  • @TheIncorrigible1 Nope, I am quite new in PS world. Checking ArgumentCompleter now. Thank you! – verte Mar 26 '19 at 16:32
  • @LotPings Turns out Mike has already edited it. Thank you Mike too! – verte Mar 26 '19 at 16:33
  • Just curious as to why you choose class verses the built-in DynamicParameter approach for a validateset / param completion. – postanote Mar 26 '19 at 21:36
  • @postanote Since I am not a windows developer, I checked it online, came across with `ValidateSet`, and it lead me to IValidateSetValuesGenerator. This is the only reason I ended up with it. However, Register-ArgumentCompleter does the trick, so I diverted that direction. – verte Mar 26 '19 at 22:05
  • Understood, but as a full edification effort, maybe for later use, take a look at the built-in DynamicParameter for validateset as well. See this one start with to see what it is about and how one might use it. --- https://martin77s.wordpress.com/2014/06/09/dynamic-validateset-in-a-dynamic-parameter – postanote Mar 27 '19 at 00:24
  • @postanote Cool! Thanks for the link, appreciate it! – verte Mar 27 '19 at 01:21

0 Answers0