【问题标题】:Which adusers in a specific department are managers of Exchange Distribution groups?特定部门中的哪些广告用户是 Exchange 通讯组的经理?
【发布时间】:2019-01-29 12:12:08
【问题描述】:

我在我们的广告中列出了用户:

Get-ADUser -Filter * -Properties department |
    Where-Object {$_.department -Like "F0*"} |
    Select sAMAccountName, department

它输出所有感兴趣的用户。

现在我想查看这些用户,并找出他们都是一个或多个 Exchange 通讯组的管理员。并且有一个用户名和分发组名称的输出,这可能吗?

【问题讨论】:

  • Get-DistributionGroup 有一个 managedby 变量,用于存储每个组的经理。这应该可以帮助您创建一个循环来实现您的预​​期输出。

标签: powershell active-directory exchange-server


【解决方案1】:

正如 Paxz 已经评论的那样,您应该对此采取“另一种方式”。如果您想获取通讯组经理的姓名和部门,请先获取这些信息。

这样的事情可能会做到:

# get the distribution groups where the ManagedBy property is set
Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
    # then go through all possible listed managers and get their DisplayName and Department
    foreach ($id in $_.ManagedBy) {
        try {
            # use '-ErrorAction Stop' to make sure the catch block is entered upon failure
            $manager = Get-AdUser -Identity $id -Properties DisplayName, Department -ErrorAction Stop
            $mgrName = $manager.DisplayName
            $mgrDept = $manager.Department
        }
        catch {
            # output failed result(s)
            $mgrName = 'Obsolete user'
            $mgrDept = 'Unknown'
        }
        # output the result(s) as PSObjects
        New-Object -TypeName PSObject -Property @{
            'Distribution Group' = $_.Name
            'Manager'            = $mgrName
            'Department'         = $mgrDept
        }
    }
}

如果您想将结果存储在 csv 文件中,可以将脚本扩展为如下内容:

$fileName = '<Enter the full path and filename here for the output csv file>'
# collect all results in an array
$results = @()
Get-DistributionGroup | Where-Object { $_.ManagedBy } | ForEach-Object {
    foreach ($id in $_.ManagedBy) {
        try {
            # use '-ErrorAction Stop' to make sure the catch block is entered upon failure
            $manager = Get-AdUser -Identity $id -Properties DisplayName, Department -ErrorAction Stop
            $mgrName = $manager.DisplayName
            $mgrDept = $manager.Department
        }
        catch {
            # output failed result(s)
            $mgrName = 'Obsolete user'
            $mgrDept = 'Unknown'
        }
        $results += New-Object -TypeName PSObject -Property @{
            'Distribution Group' = $_.Name
            'Manager'            = $mgrName
            'Department'         = $mgrDept
    }
}

$results | Export-Csv -Path $fileName -UseCulture -NoTypeInformation -Force

【讨论】:

  • 非常感谢,Get-Aduser 似乎有问题,当找到一个过时的对象时它会停止。 (Get-AdUser : Cannot find an object with identity:) ,我尝试过 -Erroraction SilentlyContinue 但它不起作用,试图获取 Try/Catch 但无法弄清楚如何写入。
  • 我已经编辑了我的答案,通过添加 try{}..catch{} 块来捕获过时的用户
猜你喜欢
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多