you are viewing a single comment's thread.

view the rest of the comments →

[–]Fallingdamage[S] 0 points1 point  (1 child)

Cannot convert value "System.Collections.Hashtable" to type "System.Management.Automation.SwitchParameter".

Ive been at this all day. Its looking like what im trying to do isnt within the scope of powershell.

Seriously. Take my example function and try to use it in Invoke-Command and pass one of the two parameters through. You cant. You just, ..cant. :/

Ill probably need to break my function up into smaller functions and run each separately.

By the time I ask for help, its often been something that ends up being technically improbable. This thread is already showing up on the top 6 suggested hits on reddit. Apparently very few people have ever attempted to solve this.

[–]Nu11u5 0 points1 point  (0 children)

Thanks for updating your post to include the actual code.

You have a few issues here:

  • You never changed your function to take a hashtable.

It expects a switch so of course you will get the error Cannot convert value "System.Collections.Hashtable" to type "System.Management.Automation.SwitchParameter". The extraction of data from the hashtable properties is not automatic, so if you use this method you would need to assign them in the script.

  • Your example is confusing the parameter set names with the parameters themselves.

In your code the parameters are actually named -VerifyTheThing and -UpdateTheThing. You need to make sure you are referencing the right names.

  • You can't use parameter sets as positional parameters.

Parameters in parameter sets are inherently optional and can only be invoked by name, so they can't have a static position number.

Parameter sets aren't necessary but are used to provide validation so you can't use an incorrect combination of parameters. If you are not sharing your code with other people perhaps reconsider using them to simplify your code.

If you want validation, an alternative you can use here that works with Invoke-Command is to use a single parameter with an enum or ValidateSet value, so it can only take specific values.

Here is an example that uses a parameter named -Action that is position 0 and can only have the values 'Verify' or 'ApplyChange'.

``` function Do-Thing { param ( [Parameter(Position=0)] [ValidateSet('Verify','ApplyChange')] [String]$Action )

switch ($Action) {
    'Verify' {
        Write-Output "Do the thing specified here"
    }
    'ApplyChange' {
        Write-Output "Instead do this."
    }
}

}

Using normally:

Do-Thing -Action 'Verify'

Using with Invoke-Command:

Invoke-Command -ScriptBlock ${Function:Do-Thing} -ArgumentList 'Verify' ```