SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Duran
Duran
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7920 Visits: 1901
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search