如何使用PowerShell变量作为命令参数?

时间:2022-06-01 19:59:36

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:

我正在尝试使用变量作为命令的参数,但无法弄明白。假设MyCommand将接受两个参数:option1和option2,它们接受布尔值。我如何使用$ newVar替换选项1或2?例如:

$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'.

我不断得到一些东西:“无法找到接受参数的位置参数”-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.

更具体地说:这里,CSV文件是不同策略的输出。循环遍历文件中的每个属性,并在我的策略asdf中设置该值;所以 - $ _。name:$ _。value应替换为-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
    }}
}

4 个解决方案

#1


19  

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

通常使用变量来填充cmdlet参数,您将使用哈希表变量,并使用@展开它

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

Added example:

添加示例:

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

Set-ActiveSyncMailboxPolicy @AS_policy1

#2


3  

See if this works for you:

看看这是否适合你:

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

#3


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.

无法工作,因为&符运算符只执行没有参数或脚本块的单个命令。

#4


1  

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

我有同样的问题,只是发现了如何解决它。解决方案是使用invoke-Expression:invoke-Expression $ mycmd这使用$ mycmd-string,替换变量并将其作为cmdlet用给定参数执行

#1


19  

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

通常使用变量来填充cmdlet参数,您将使用哈希表变量,并使用@展开它

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

Added example:

添加示例:

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

Set-ActiveSyncMailboxPolicy @AS_policy1

#2


3  

See if this works for you:

看看这是否适合你:

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

#3


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.

无法工作,因为&符运算符只执行没有参数或脚本块的单个命令。

#4


1  

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

我有同样的问题,只是发现了如何解决它。解决方案是使用invoke-Expression:invoke-Expression $ mycmd这使用$ mycmd-string,替换变量并将其作为cmdlet用给定参数执行