新邮箱命令不接受 -Equipment 参数



我正在尝试通过脚本在Exchange Online中创建新资源,如果我手动键入该行,它可以工作,但是当我运行脚本时,命令New-Mailbox突然无法接受"-Equipment"参数。

脚本在以下行上失败:

New-Mailbox -Name "$($Resource)" -$($Type)

错误显示以下内容:

A positional parameter cannot be found that accepts argument '-Equipment'.
 + CategoryInfo          : InvalidArgument: (:) [New-Mailbox], ParameterBindingException"

PowerShell 将-$($Type)解释为字符串参数而不是参数名称。使用拼接有条件地传递参数,如下所示:

$extraParams = @{ $Type = $true }
New-Mailbox -Name "$($Resource)" @extraParams

我不确定 Exchange Online 中还有哪些其他类型的邮箱可用,但您可能需要弄清楚并应用一些输入验证:

param(
    [string]$Resource,
    [ValidateSet('Equipment','Person','Room')]
    [string]$Type
)
# do other stuff here
# If someone passed a wrong kind of `$Type`, the script would have already thrown an error
$extraParams = @{ $Type = $true }
New-Mailbox -Name "$($Resource)" @extraParams

最新更新