diff --git a/powershell/backup-groupmembers.ps1 b/powershell/backup-groupmembers.ps1 index 46c68dd..c30532a 100644 --- a/powershell/backup-groupmembers.ps1 +++ b/powershell/backup-groupmembers.ps1 @@ -2,17 +2,26 @@ $global_catalog = Get-ADDomainController -Discover -Service GlobalCatalog $server = "$($global_catalog.Name):3268" -$group = "" +$groups = @("", "") -$csv_file = "$($group) ($(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss')).csv" +$csv_file = "backup_$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').csv" try { + foreach ($group in $groups) { $ad_group = Get-ADGroup -Filter 'Name -eq $group -and GroupScope -eq "DomainLocal" -and GroupCategory -eq "Security"' -Server $server -Properties Member -ErrorAction Stop - $group_members = $ad_group.Member - $group_members | Export-Csv -Path $csv_file -NoTypeInformation + $csv_export_table = @() + $ad_group.Member | ForEach-Object { + $ad_user = Get-ADUser -Identity $_ -Properties * -Server $server + $csv_export_table += [PSCustomObject]@{ + Group = $group + MemberEmail = $ad_user.SamAccountName + MemberDistinguishedName = $ad_user.DistinguishedName + } + } + } } catch { Write-Error "Group $group not found" exit 1 } - +$csv_export_table | Export-Csv -Path $csv_file -NoTypeInformation \ No newline at end of file