简体   繁体   中英

Using a Variable as a Parameter - Powershell

I am trying to pass a variable through to be used as a parameter within a function. I'm not even sure if this is possible but below is what i am attempting to accomplish, what i have tried so far keeps kicking out a "positional parameter cannot be found that accepts argument" error

$Var = Read-host "enter Attribute number"
$CustomAtt = "CustomAttribute$Var"

Get-Mailbox -Identity $Email | Set-Mailbox -$CustomAtt "TestTest"

You cannot set cmdlet arguments that way in powershell. You can do what your are attempting to do by using a feature called argument splatting . Simply store your arguments in an array or hashtable and then apply them to the cmdlet using @ symbol.

Like this:

$mailBoxAttributes = @{
    $CustomAtt = "TestTest" }

Get-Mailbox -Identity $Email | Set-Mailbox @mailBoxAttributes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM