简体   繁体   English

试图找到一个部门中的所有用户,并比较他们的 AD 组

[英]Trying to find all users in a department, and comparing their AD groups

I'm trying to find all users in a department, and comparing their AD groups, to see if there is any ad groups in common.我试图找到一个部门中的所有用户,并比较他们的广告组,看看是否有任何共同的广告组。

I'm a bit new to writing this kind of scripts - hope you can help me.我对编写这种脚本有点陌生 - 希望你能帮助我。

import-module activedirectory

$afd = "Wright the name of the department you want to compair"
$users = get-ADUser -Filter * -Properties department | Where-Object { $_.department -Like "$afd*" } | Select sAMAccountName, department

$users | Sort-Object -Property department | select samaccountname

ForEach ($user in $users) 
{
    
}

This is where I get stuck, i get til sorted list and would like to see all the ad groups in all the different departments and compare them.这就是我卡住的地方,我得到了排序列表,并希望查看所有不同部门的所有广告组并进行比较。

Does anyone have an idea or hint to get me going again?有没有人有想法或暗示让我再次出发?

Regards Simeon问候西蒙

A potentially slower but simpler way is to utilize Get-ADPrincipalGroupMembership .一种可能较慢但更简单的方法是利用Get-ADPrincipalGroupMembership

ForEach ($user in $users) 
{
    $UserGroups = (Get-ADPrincipalGroupMembership $user.SamAccountName).Name
    [pscustomobject]@{
        User = $user.SamAccountName
        GroupsList = $UserGroups # Keeping Groups as a collection
        GroupsString = $UserGroups -join ';' # Delimited (;) string of the groups
}

If you use the memberOf property of your user objects, you will already have a groups list (only the distinguishednames as strings).如果您使用用户对象的memberOf属性,您将已经有了一个组列表(只有区分名称作为字符串)。 However, it may be harder to work with later in your code because the values will only be strings.但是,稍后在您的代码中可能更难使用,因为这些值只会是字符串。

$users = get-ADUser -Filter * -Properties department,memberOf |
    Where-Object { $_.department -Like "$afd*" } |
        Select sAMAccountName,department,memberOf

In either case, storing a property value as an array is great for accessing those values later in code.在任何一种情况下,将属性值存储为数组非常适合稍后在代码中访问这些值。 Because $user.memberOf for example will enumerate all the group DN strings for you.因为例如$user.memberOf将为您枚举所有组 DN 字符串。 However, it won't work for exporting to a CSV as the property values are expected to be single strings.但是,它不适用于导出到 CSV,因为属性值应该是单个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM