簡體   English   中英

使用變量作為參數 - Powershell

[英]Using a Variable as a Parameter - Powershell

我正在嘗試傳遞一個變量以用作 function 中的參數。 我什至不確定這是否可能,但下面是我想要完成的,到目前為止我嘗試過的一直踢出“找不到接受參數的位置參數”錯誤

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

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

您不能在 powershell 中以這種方式設置 cmdlet arguments。 您可以通過使用稱為參數噴射的功能來完成您正在嘗試做的事情。 只需將 arguments 存儲在數組或哈希表中,然后使用@符號將它們應用到 cmdlet。

像這樣:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM