【问题标题】:Powershell - Get User information from AD listPowershell - 从 AD 列表中获取用户信息
【发布时间】:2020-11-20 16:40:35
【问题描述】:

我是一般编程的初学者.. 我正在尝试做的是创建一个 powershell 脚本,它将:

  • 获取有关 Active Directory 组中每​​个用户的信息。

  • 每个组内可能还有另一个组,所以我希望它也能从每个嵌套组中获取用户列表。

  • 每个组只给我一次信息。

这是我目前所拥有的:

$list = Get-ADGroupMember Admins

foreach($u in $list) {
    Get-ADObject $u
}

foreach ($_ in $u) {
    if ($u.ObjectClass -eq 'user') { 
        Get-ADUser $u -Properties * | select givenname, surname, samaccountname | ft -autosize
    } else { 
        Get-ADGroupMember $u -Recursive | select name, samaccountname | ft -autosize
    }
}

到目前为止,我正试图让它与那个“管理员”组一起工作,然后如果可以的话,我想同时为更多组运行代码。

任何帮助或指导将不胜感激。

【问题讨论】:

  • 为什么有两个foreach 循环?看来他们应该结合起来。
  • 如果我这样做,它可以工作,但它为每个用户提供了一行。有没有办法把它合并到一个列表中?
  • 我们还没有收到您的来信。我的回答解决了你的问题吗?作为 SO 的新手,您可能不知道这一点,但习惯于单击左侧的 ✓ 图标 accept the answer that solved your problem。这将帮助其他有类似问题的人更轻松地找到它,并有助于激发人们回答您的问题。

标签: powershell active-directory usergroups


【解决方案1】:

您似乎只需要Get-ADUserGet-ADGroup 默认返回的属性,因此在这两种情况下,都无需指定-Properties 参数。

Get-ADGroupMember 可以返回用户、计算机和组对象,因此目前,您的 else 条件需要组,您最终可能会得到一个计算机对象。

在您的代码中,您在ifelse 中都使用ft -autosize 输出到控制台,但是在循环开始时在变量中捕获这两种类型的结果对象并输出会更简单之后的整体:

# you can load a list of group names from a predefined array:
$Groups = 'Admins', 'Users'

# or load from a file, each group name listed on a separate line:
# $Groups = Get-Content -Path 'D:\Test\ADGroups.txt'

# or get all AD groups in the domain:
# $Groups = (Get-ADGroup -Filter *).Name


$result = foreach ($group in $Groups) {
    Get-ADGroup -Filter "Name -eq '$group'" | ForEach-Object {
        # we could use the $group variable, but this ensures correct casing
        $groupName = $_.Name
        $members = $_ | Get-ADGroupMember -Recursive
        foreach ($member in $members) {
            if ($member.objectClass -eq 'user') {
                Get-ADUser -Identity $member.DistinguishedName |
                Select-Object @{Name="GroupName"; Expression={$groupName}},
                              @{Name="MemberType";Expression={'User'}},
                              Name, 
                              GivenName, 
                              Surname, 
                              SamAccountName
            }
            elseif ($member.objectClass -eq 'group') {
                Get-ADGroup -Identity $member.DistinguishedName |
                Select-Object @{Name="GroupName";Expression={$groupName}},
                              @{Name="MemberType";Expression={'Group'}},
                              Name,
                              @{Name="GivenName";Expression={''}},  # groups don't have this property
                              @{Name="Surname";Expression={''}},    # groups don't have this property
                              SamAccountName
            }
        }
    }
}

# output is console
$result | Format-Table -AutoSize

# write to CSV file
$result | Export-Csv -Path 'D:\Test\GroupsInfo.csv' -NoTypeInformation

这里的诀窍是为用户和组对象输出具有相同属性的对象

【讨论】:

  • 在看到您的评论之前,我已经解决了这个问题,但您的答案与我得到的答案非常接近。感谢您的帮助!
猜你喜欢
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多