Powershell experts, I need help with dynamic parameters. Posh V5. First Param Determines list of choices in next param.

  • Hello,

    I am really trying to get my head around dynamic parameters, but I just dont know enough PS to get this right. I'd like to select an environment from a drop down (validateset, no issues here), but then I'd like a related list to become available in the next parameter, in other works..

    -Env1 <list of environments> (I pick env1) -servers <list of env1 servers>

    or if I chose env2, the -servers parameter would present me with a list of servers in env2

    ...I dont mind typing in a list of available servers for each environment, I'll come up with something for that later. So this is what I have so far, that does not work, here I am just trying to get it to work for just one of the choices to get me started..

    Param(
    [Parameter(Mandatory = $true, Position=0, HelpMessage="Choose your source environment")]
    [ValidateSet("Env1", "Env2")][string]$Enviroment,

    [Parameter(Mandatory = $true, Position=1)]
    [ValidateSet(fn_if_env)] $servers
    )

    function fn_if_env ($Enviroment)
    {
    if( $Enviroment -eq "Env1")

    {

    $ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

    } if ($Environmet -eq "Env1") {

     
      $servers = New-Object System.Management.Automation.ParameterAttribute -Property @{
      Position = 3
      Mandatory = $true,
      [ValidateSet("Server1","server2","Server3")][string]$servers
      }
     
      $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
      $attributeCollection.Add($EnvProdAttribute)
      $EnvProdAttribute = New-Object System.Management.Automation.RuntimeDefinedParameter('Env1', [String], $attributeCollection)
      $paramDictionary.Add('Server1', $servers)
      $paramDictionary.Add('Server2', $servers)
      $paramDictionary.Add('Server3', $servers)    }
    }

    ...am I even close? Would love some help, I have found examples, but its normally the same one on different sites, and does not match very closely to what I want to do it. for example, I dont understand where I am supposed to put the servers options after the initial choice of environment, I'm fairly sure I've got wrong here.

    Thank you all in advance.

    Regards,
    D.

Viewing 0 posts

You must be logged in to reply to this topic. Login to reply