【问题标题】:New-Mailbox command does not accept the -Equipment argumentNew-Mailbox 命令不接受 -Equipment 参数
【发布时间】:2019-06-19 11:05:54
【问题描述】:

我正在尝试通过脚本在 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 office365 exchange-server


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 2013-05-23
      • 2013-11-24
      • 2015-06-21
      相关资源
      最近更新 更多