New-Mailbox命令不接受-Equipment参数

时间:2019-06-19 11:05:54

标签: powershell office365 exchange-server

我试图通过脚本在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"

1 个答案:

答案 0 :(得分:2)

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

$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