【问题标题】:Checking if Distribution Group Exists in Powershell检查Powershell中是否存在通讯组
【发布时间】:2012-08-28 20:33:17
【问题描述】:

我正在编写一个脚本来快速创建一个新的通讯组并用 CSV 填充它。我无法测试组名是否已存在。

如果我执行get-distributiongroup -id $NewGroupName 并且它不存在,我会得到一个异常,这是我期望发生的。如果该组确实存在,那么它会列出该组,这也是我所期望的。但是,在尝试创建组之前,我找不到测试组是否存在的好方法。我尝试过使用 try/catch,也这样做了:

Get-DistributionGroup -id $NewGroupName -ErrorAction "Stop" 

这使得 try/catch 工作得更好(据我所知,非终止错误)。

基本上,我需要让用户输入一个新的组名来检查它是否可行。如果是,则创建该组,如果不是,则应提示用户输入另一个名称。

【问题讨论】:

    标签: powershell exchange-server


    【解决方案1】:

    您可以使用SilentlyContinue erroraction 以便不显示异常/错误:

    $done = $false
    while(-not $done)
    {
       $newGroupName = Read-Host "Enter group name"
       $existingGroup = Get-DistributionGroup -Id $newGroupName -ErrorAction 'SilentlyContinue'
    
       if(-not $existingGroup)
       {
          # create distribution group here
          $done = $true
       }
       else
       {
          Write-Host "Group already exists"
       }
    }
    

    【讨论】:

      【解决方案2】:

      这应该可以解决问题:

      ((Get-DistributionGroup $NewGroupName -ErrorAction 'SilentlyContinue').IsValid) -eq $true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-19
        • 2019-07-23
        • 1970-01-01
        • 2017-09-24
        • 2015-04-10
        • 2015-10-31
        • 2018-10-18
        • 1970-01-01
        相关资源
        最近更新 更多