Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a PowerShell variable as command parameter?

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
    }}
}
like image 712
Gary Avatar asked May 10 '11 21:05

Gary


People also ask

How do you pass a variable in PowerShell?

To pass a variable to a parameter that expects a reference, you must type cast your variable as a reference. The brackets and parenthesis are BOTH required.

How do you use parameters in PowerShell?

The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.

How do I run a PowerShell script from the command line with parameters?

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.


3 Answers

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
like image 67
mjolinor Avatar answered Oct 25 '22 02:10

mjolinor


See if this works for you:

 iex "MyCommand -$($newVar):$true"
like image 45
manojlds Avatar answered Oct 25 '22 03:10

manojlds


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

like image 32
residuum Avatar answered Oct 25 '22 01:10

residuum