导出广告组成员资格-Excel多个工作表

时间:2019-07-26 10:27:05

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

是否可以导出到制表符(表格)上的excel?例如,每个选项卡(工作表)将是一个安全组并列出成员吗?但是它不能正常工作。 我们正在从这里使用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

所需的输出:

我想让ADgroup中的所有用户使用显示名,名称和邮件。

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

预先感谢

1 个答案:

答案 0 :(得分:2)

我没有您的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
}

希望有帮助