导出 AD 组成员资格 - Excel 多张工作表

Export AD Group Membership - Excel Multiple Sheets

有没有办法在选项卡 (sheets) 上导出到 excel?例如,每个选项卡 (sheet) 都是一个安全组和列出的成员?但它不能正常工作。 我们从这里使用 ImportExcel 模块:https://github.com/dfinke/ImportExcel .

到目前为止我已经试过了:

Get-ADGroup -Properties name -Filter 'name -like "*VPN*"' | % { Get-ADGroupMember -Identity $_.Name | Export-Excel -Path C:\tmp\Groups.xlsx -WorkSheetname $_.Name -AutoSize }

输出:

distinguishedName,name,objectClass,objectGUID,SamAccountName,SID,PropertyNames,AddedProperties,RemovedProperties,ModifiedProperties,PropertyCount
CN=User,DC=contoso,DC=com,John T,user,1bae7a5f-9f19-4435-a47d-66855024cea8,johnt,S-1-5-21-3248419837-683404524-394759481-53949,System.Collections.Generic.SortedDictionary`2+KeyCollection[System.String,Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]    System.Collections.Generic.HashSet`1[System.String] System.Collections.Generic.HashSet`1[System.String] System.Collections.Generic.HashSet`1[System.String] 6

期望的输出:

我想获取 AD 组中的所有用户以及显示名称、姓名和邮件。

Displayname , Name , Mail
John T,john.t, johnt@contoso.com

提前致谢,

我没有你的 Export-Excel 功能,所以下面的代码导出到 CSV:

# 1. Get the groups that match the name pattern
# 2. Get the members of these groups recursive
# 3. Select only member sthat are users.
# 4. Get the user properties you want in the output
# 5, Store it all in a variable $result.
# 6. Export the $result to CSV (or use Export-Excel)
$result = Get-ADGroup -Properties Name -Filter 'name -like "*VPN*"' | ForEach-Object { 
    $group = $_.Name
    Get-ADGroupMember -Identity $group -Recursive | 
    Where-Object {$_.objectClass -eq 'user'} |
    Get-ADUser -Properties Displayname,Name,EmailAddress |
    Select-Object @{Name = 'Group'; Expression = {$group}}, Displayname,Name,EmailAddress

$result | Export-Csv -Path 'C:\tmp\Groups.csv' -NoTypeInformation

要将每个组保存到新文件或工作表,请使用:

# Group the results by the Group name
$result | Group-Object -Property Group | ForEach-Object {
    $group = $_.Name
    $_.Group | Select-Object * -ExcludeProperty Group | 
    # save as separate csv files
    # Export-Csv -Path "C:\tmp$group.csv" -NoTypeInformation

    # or as separate worksheets in an Excel document
    Export-Excel -Path 'C:\tmp\Groups.xlsx' -WorkSheetname $group -AutoSize
}

希望对您有所帮助