25

I'm trying to use a variable as a command's parameter but can't quite figure it out. Let's say MyCommand will accept two parameters: option1 and option2 and they accept boolean values. How would I use $newVar to substitute option 1 or 2? For example:

$newVar = "option1"
MyCommand -$newVar:$true

I keep getting something along the lines of 'A positional parameter cannot be found that accepts argument '-System.String option1'.


More Specifically:
Here, the CSV file is an output of a different policy. The loop goes through each property in the file and sets that value in my policy asdf; so -$_.name:$_.value should substitute as -AllowBluetooth:true.

Import-Csv $file | foreach-object {
    $_.psobject.properties | where-object {
    # for testing I'm limiting this to 'AllowBluetooth' option
    if($_.name -eq "AllowBluetooth"){
    Set-ActiveSyncMailboxPolicy -Identity "asdf" -$_.name:$_.value
    }}
}
1
  • 2
    You mean newVar but wrongly put in myVar? Commented May 10, 2011 at 21:49

5 Answers 5

35

Typically to use a variable to populate cmdlet parameters, you'd use a hash table variable, and splat it, using @

 $newVar = @{option1 = $true}
 mycommand @newVar

Added example:

$AS_policy1 = @{
Identity = "asdf"
AllowBluetooth = $true
}

Set-ActiveSyncMailboxPolicy @AS_policy1
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help, but that returns an error "A positional parameter cannot be found that accepts argument 'System.Collections.Hashtable'. Specifically I have a loop to get properties properties from a Csv and run them into Set-ActiveSyncMailboxPolicy. For a specific instance I'll say $_.name = "AllowBluetooth" and $_.value = $true (I wrote these variables out to confirm correct values. However, I can't quite put it all together Set-ActiveSyncMailboxPolicy -Identity "asdf" @{$_.name = $_.value} or -$_.name:$_.value. It takes $_.value just fine though.
See edit. If you splat the parameters, you should include all of them in the hashtable.
If anyone is trying to use this answer and the example is not working for you, maybe you're doing the same mistake I did, for lack of attention: using the variable in the usual way writing $AS_policy1 when I should be writing @AS_policy1 with a "splatting" ampersand.
7

See if this works for you:

 iex "MyCommand -$($newVar):$true"

3 Comments

Thanks manojlds, this got me a little closer. Literally, $newVar is $_.name and $true is $_.value (which equals "Allow"). The way I was doing it, it accepted $_.value for the value but not $_.name as a parameter. Using your method accepts $_.name as my parameter but not $_.value as the value... Saying cannot convert value "System.String" to type [type]. I'll keep trying other things though.
I got your method to work when -$($newVar) wants a string, but it fails on boolean values because it thinks it's a string. I believe it's because the whole command is in quotes. Any pointers?
@Fantabulum: iex "MyCommand -$($_.name):$($_.value)" should work then, for the reason I've explained in my answer. +1 @manojlds.
5

I had the same Problem and just found out how to resolve it. Solution is to use invoke-Expression: invoke-Expression $mycmd This uses the $mycmd-string, replaces variables and executes it as cmdlet with given parameters

Comments

4

Nowadays, If you don't mind evaluating strings as commands, you may use Invoke-Expression:

$mycmd = "MyCommand -$($newVar):$true"
Invoke-Expression $mycmd

Comments

1

I would try with:

$mycmd = "MyCommand -$($newVar):$true"
& $mycmd

result

Can't work because the ampersand operator just execute single commands without prameters, or script blocks.

1 Comment

Thanks for the suggestion empo, but this seems to behave the same way as my original. I can post an excerpt of my actual script if that would help, but it involves several loops and Exchange commands so I think it would just cause confusion.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.