0

I'm trying to use a commandlet (I'm using NavContainerHelper) dynamically.

My parameters are in a System.Collections.Generic.List with the type string:

$navContainerHelperParameters = New-Object 'System.Collections.Generic.List[string]'
$navContainerHelperParameters.Add("-accept_eula")

I already tried using the @ before the call of the commandlet: New-NavContainer @navContainerHelperParameters, but this did not work as expected.

What would be the best way to call a commandlet dynamically?

5
  • Take a look at Invoke-Command and the -Argumentlist option. Commented Jun 27, 2018 at 6:26
  • I don't think that Invoke-Command would help me with my problem. It seems that it would only be good if I'm using something like -containerName $containerName and $containerName is out of scope Commented Jun 27, 2018 at 6:38
  • Perhaps it's just me but I fail to see anything dynamic about calling a cmdlet with one switch parameter. Just run New-NavContainer -accept_eula. Commented Jun 27, 2018 at 14:19
  • @AnsgarWiechers that's just an example. In my script I have more parameters that are dynamically added Commented Jun 28, 2018 at 6:06
  • Next time please choose an example that actually demonstrates the problem you're facing. Commented Jun 28, 2018 at 7:00

1 Answer 1

1

The best way is to use splatting, which requires the parameters to be set in a hashtable and then passed with @ infront of the variable name to the cmdlet. You can set the switch parameter by setting the value to $true.

$navContainerHelperParameters = @{}
[void]$navContainerHelperParameters.Add("accept_eula",$true)
New-NavContainer @navContainerHelperParameters
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.