feat: Add script to backup group members via csv file

This commit is contained in:
Björn Benouarets
2025-08-13 09:12:31 +02:00
parent ad4125159a
commit bc79025582

View File

@@ -6,10 +6,13 @@ $groups = @("", "")
$csv_file = "backup_$(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
$csv_export_table = @() $csv_export_table = @()
foreach ($group in $groups) {
try {
$ad_group = Get-ADGroup -Filter 'Name -eq $group -and GroupScope -eq "DomainLocal" -and GroupCategory -eq "Security"' -Server $server -Properties Member -ErrorAction Stop
if ($ad_group.Member) {
$ad_group.Member | ForEach-Object { $ad_group.Member | ForEach-Object {
try { try {
$ad_user = Get-ADUser -Identity $_ -Properties * -Server $server -ErrorAction Stop $ad_user = Get-ADUser -Identity $_ -Properties * -Server $server -ErrorAction Stop
@@ -19,13 +22,20 @@ try {
MemberDistinguishedName = $ad_user.DistinguishedName MemberDistinguishedName = $ad_user.DistinguishedName
} }
} catch { } catch {
Write-Error "User $group not found and skipped..." Write-Warning "User $_ not found in group $group - skipping..."
} }
} }
} else {
Write-Warning "Group $group has no members"
} }
} catch { } catch {
Write-Error "Group $group not found" Write-Warning "Group $group not found - skipping..."
exit 1 }
} }
if ($csv_export_table.Count -gt 0) {
$csv_export_table | Export-Csv -Path $csv_file -NoTypeInformation $csv_export_table | Export-Csv -Path $csv_file -NoTypeInformation
Write-Host "Backup completed successfully. Exported $($csv_export_table.Count) members to $csv_file"
} else {
Write-Warning "No members found to export"
}