简体   繁体   English

使用变量作为参数 - Powershell

[英]Using a Variable as a Parameter - Powershell

I am trying to pass a variable through to be used as a parameter within a function.我正在尝试传递一个变量以用作 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.您不能在 powershell 中以这种方式设置 cmdlet arguments。 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.只需将 arguments 存储在数组或哈希表中,然后使用@符号将它们应用到 cmdlet。

Like this:像这样:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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