广告组和用户

时间:2019-02-12 19:06:10

标签: powershell active-directory export-to-excel

我需要将以下内容转换为CSV:

具有以下字段的网上论坛:网上论坛名称,网上论坛SID,网上论坛电子邮件地址,网上论坛类型

然后对于上述每个组,我需要具有以下字段的成员用户:组名(我知道这是重复的),用户ID,用户名,用户姓氏,用户电子邮件。

如果有人对此有解决方案,我将永远感激不已。解决方案将是我可以学习和学习的东西,再次感谢。

我分别拥有两件,但当时卡住了

Get-ADGroup -Filter * -Properties Member |
    select Name, DistinguishedName, sid, GroupCategory, GroupScope,
        @{Name="Members";Expression={($_.Members | Measure-Object).Count}} |
    Out-GridView
    #Export-Csv c:\rmm-mgmt\test.csv

我不明白为什么不能只添加用户详细信息。 对于我正在使用的用户:

Get-ADUser -Filter * -Properties * |
    Select-Object name, surname, givenname, displayname, emailaddress |
    Out-GridView

(在开始导出之前使用Out-GridView检查结果)

您可以看到这是我可以获得但不能将它们汇总在一起的两条信息。一个示例是我无法获取组中的成员列表。

2 个答案:

答案 0 :(得分:0)

您必须使用ForEach-Object并将该组分配给一个命名变量,以便可以在格式化每个成员的位置访问它。然后将$_.Member用管道输送到Get-ADUser中:

Get-ADGroup -Filter * -Properties Member | ForEach-Object {
    $group = $_
    $_.Member | Get-ADUser -Properties Surname,GivenName,DisplayName,EmailAddress |
        Select @{N = "Group Name";E = {$group.Name}}, Surname, GivenName, DisplayName, EmailAddress
} | Out-GridView

答案 1 :(得分:0)

对于所要查找的确切输出格式,我不太清楚,但是我相信类似的东西应该可以满足您的尝试。

# Create an empty array we will add our custom objects to.
$outputObjs = @()

# Get all the AD groups.
$groups = Get-ADGroup -Filter * -Properties Member | 
            select Name, DistinguishedName, sid, GroupCategory, GroupScope |
            select -First 40

# Iterate the groups capturing the members of each group.
foreach ($group in $groups) {
    $groupMembers = Get-ADGroupMember -Identity $group.Name |
                        ? { $_.objectClass -eq "user" } | 
                        % { Get-ADUser $_.SamAccountName } | 
                        select name, surname, givenname, displayname, emailaddress

    # Get the group member count
    $memCount = 0
    if ($groupMembers) { $memCount = $groupMembers.Count; } 
    else { $memCount = 0 }

    # Iterate through the group members creating a custom object and adding it to our object array.
    foreach($member in $groupMembers) {
        $outputObjs += New-Object -TypeName psobject -Property (@{
            'Group Name' = $group.Name;
            'Group DN' = $group.DistinguishedName;
            'Group SID' = $group.SID;
            'Group Category' = $group.GroupCategory;
            'Group Scope' = $group.GroupScope;
            'Group Member Count' = $memCount
            'User Name' = $member.name;
            'User Surname' = $member.surname;
            'User Given Name' = $member.givenname;
            'User Display Name' = $member.displayname;
            'User Email Address' = $member.emailaddress
        })
    }
}

# Convert the object array to a CSV.
$outputObjs | ConvertTo-Csv -NoTypeInformation

您的CSV看起来像这样

"Group Name","User Surname","User Given Name","Group Member Count","User Email Address","Group SID","User Name","Group Category","Group DN","User Display Name","Group Scope"