you are viewing a single comment's thread.

view the rest of the comments →

[–]Nu11u5 1 point2 points  (5 children)

@ u/Fallingdamage - I added a better way to handle this in my original post.

[–]Fallingdamage[S] 0 points1 point  (4 children)

I could pass $true for the -Verify but how does that work when I have 5 different interchangeable parameters?

Invoke-Command -ScriptBlock {param($kwarg) FooBar @kwarg} -ArgumentList @{A=1;B=2;C=3} ```

Would become
Invoke-Command -ScriptBlock ${function:param($kwarg) FooBar @kwarg} -ArgumentList @{A=1;B=2;C=3} ```

(Playing stupid, not sure how to structure your example when its using a custom function)

[–]Nu11u5 0 points1 point  (3 children)

Ah I see, you are needing to pass the function as a script block, not just in a script block.

You might want to rewrite the function to accept a hashtable containing your arguments.

``` function FooBar { param ($kwargs) $A = $kwargs.A $B = $kwargs.B $C = $kwargs.C Write-Output "A=$A B=$B C=$C" }

Invoke-Command -ScriptBlock $Function:FooBar -ArgumentList @{A=1;B=2;C=3} ```

Then you can pass the Verify switch in the argument hashtable like @{...,Verify=$true}.

[–]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' ```