Home > Software engineering >  Using a Variable as a Parameter - Powershell
Using a Variable as a Parameter - Powershell

Time:07-26

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"

CodePudding user response:

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
  • Related