feat: Add script to backup group members via csv file
This commit is contained in:
@@ -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 {
|
$csv_export_table = @()
|
||||||
foreach ($group in $groups) {
|
|
||||||
|
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
|
$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 = @()
|
|
||||||
|
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 {
|
||||||
|
Write-Warning "Group $group not found - skipping..."
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
Write-Error "Group $group not found"
|
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$csv_export_table | Export-Csv -Path $csv_file -NoTypeInformation
|
if ($csv_export_table.Count -gt 0) {
|
||||||
|
$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"
|
||||||
|
}
|
Reference in New Issue
Block a user