This commit is contained in:
Przemysław Kłys
2020-04-07 20:26:28 +02:00
parent 1241cf3680
commit 3fce54e75a
7 changed files with 210 additions and 127 deletions
+2 -2
View File
@@ -2,8 +2,8 @@
# Backup GPOs
$BackupPath = "$Env:UserProfile\Desktop\GPO"
$GPOSummary = Backup-GPOZaurr -BackupPath $BackupPath -Verbose -Type EmptyAndUnlinked
$GPOSummary = Backup-GPOZaurr -BackupPath $BackupPath -Verbose -Type EmptyAndUnlinked -BackupDated #-LimitProcessing 1
$GPOSummary | Format-Table -AutoSize
# Confirm GPOs are backed up properly
#Get-GPOZaurrBackupInformation -BackupFolder $GPOSummary[0].BackupDirectory | Format-Table -a
Get-GPOZaurrBackupInformation -BackupFolder $GPOSummary[0].BackupDirectory | Format-Table -a
@@ -1,12 +1,5 @@
Import-Module "$PSScriptRoot\..\GPoZaurr.psd1" -Force
# Backup GPOs
$BackupPath = "$Env:UserProfile\Desktop\GPO"
$GPOSummary = Backup-GPOZaurr -BackupPath $BackupPath -Verbose -Type EmptyAndUnlinked
$GPOSummary | Format-Table -AutoSize
# Confirm GPOs are backed up properly
#Get-GPOZaurrBackupInformation -BackupFolder $GPOSummary[0].BackupDirectory | Format-Table -a
# Remove GPOS
Remove-GPOZaurr -Type EmptyAndUnlinked -Verbose
$BackupPath = "$Env:UserProfile\Desktop\GPO"
Remove-GPOZaurr -Type EmptyAndUnlinked -BackupPath $BackupPath -BackupDated -LimitProcessing 2 -Verbose
+78 -67
View File
@@ -1,5 +1,5 @@
function Backup-GPOZaurr {
[cmdletBinding()]
[cmdletBinding(SupportsShouldProcess)]
param(
[int] $LimitProcessing,
[validateset('All', 'Empty', 'Unlinked', 'EmptyAndUnlinked')][string] $Type = 'All',
@@ -8,74 +8,85 @@
[alias('Domain', 'Domains')][string[]] $IncludeDomains,
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath,
[string] $BackupPath
[string] $BackupPath,
[switch] $BackupDated
)
# Logging Paths
$DateDirectory = "$BackupPath\$((Get-Date).ToString('yyyy-MM-dd_HH_mm_ss'))"
Write-Verbose "Backup-GPOZaurr - Backing up to $DateDirectory"
Begin {
if ($BackupDated) {
$BackupFinalPath = "$BackupPath\$((Get-Date).ToString('yyyy-MM-dd_HH_mm_ss'))"
} else {
$BackupFinalPath = $BackupPath
}
Write-Verbose "Backup-GPOZaurr - Backing up to $BackupFinalPath"
$null = New-Item -ItemType Directory -Path $BackupFinalPath -Force
$Count = 0
}
Process {
Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -GPOPath $GPOPath | ForEach-Object {
#$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation
#$GPOSummary = foreach ($GPO in $GPOs) {
#$QueryServer = $ForestInformation['QueryServers'][$_.Domain]['HostName'][0]
if ($Type -eq 'All') {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
} elseif ($Type -eq 'Empty') {
if ($_.ComputerSettingsAvailable -eq $false -and $_.UserSettingsAvailable -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'EmptyAndUnlinked') {
if ($_.ComputerSettingsAvailable -eq $false -and $_.UserSettingsAvailable -eq $false -or $_.Linked -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'Unlinked') {
if ($_.Linked -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
}
$GPOs = Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -GPOPath $GPOPath
#$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation
$Count = 0
$null = New-Item -ItemType Directory -Path $DateDirectory -Force
$GPOSummary = foreach ($GPO in $GPOs) {
#$QueryServer = $ForestInformation['QueryServers'][$GPO.Domain]['HostName'][0]
if ($Type -eq 'All') {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Name $GPO.Name -Domain $GPO.Domain -Path $DateDirectory #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain) using server $QueryServer failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
} elseif ($Type -eq 'Empty') {
if ($GPO.ComputerSettingsAvailable -eq $false -and $GPO.UserSettingsAvailable -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Name $GPO.Name -Domain $GPO.Domain -Path $DateDirectory #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain) using server $QueryServer failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'EmptyAndUnlinked') {
if ($GPO.ComputerSettingsAvailable -eq $false -and $GPO.UserSettingsAvailable -eq $false -or $Gpo.Linked -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Name $GPO.Name -Domain $GPO.Domain -Path $DateDirectory #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain) using server $QueryServer failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'Unlinked') {
if ($Gpo.Linked -eq $false) {
Write-Verbose "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
try {
$BackupInfo = Backup-GPO -Name $GPO.Name -Domain $GPO.Domain -Path $DateDirectory #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Backup-GPOZaurr - Backing up GPO $($GPO.Name) from $($GPO.Domain) using server $QueryServer failed: $($_.Exception.Message)"
}
if ($LimitProcessing -eq $Count) {
break
}
}
#}
#$GPOSummary
}
}
$GPOSummary
End {
}
}
+1
View File
@@ -16,6 +16,7 @@
if (-not $GPOPath) {
foreach ($Domain in $ForestInformation.Domains) {
Get-GPO -All -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain | ForEach-Object {
Write-Verbose "Get-GPOZaurr - Getting GPO $($_.DisplayName) / ID: $($_.ID) from $Domain"
$XMLContent = Get-GPOReport -ID $_.ID -ReportType XML -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain
Get-XMLGPO -XMLContent $XMLContent
}
+24 -16
View File
@@ -3,24 +3,32 @@
param(
[string] $BackupFolder
)
if ($BackupFolder) {
if ((Test-Path -LiteralPath "$BackupFolder\manifest.xml")) {
[xml] $Xml = Get-Content -LiteralPath "$BackupFolder\manifest.xml"
[Array] $BackupInformation = $Xml.Backups.BackupInst | ForEach-Object {
[PSCustomObject] @{
DisplayName = $_.GPODisplayName.'#cdata-section'
Domain = $_.GPODomain.'#cdata-section'
Guid = $_.GPOGUid.'#cdata-section'
DomainGuid = $_.GPODomainGuid.'#cdata-section'
DomainController = $_.GPODomainController.'#cdata-section'
BackupTime = $_.BackupTime.'#cdata-section'
ID = $_.ID.'#cdata-section'
Comment = $_.Comment.'#cdata-section'
Begin {
}
Process {
if ($BackupFolder) {
if ((Test-Path -LiteralPath "$BackupFolder\manifest.xml")) {
[xml] $Xml = Get-Content -LiteralPath "$BackupFolder\manifest.xml"
[Array] $BackupInformation = $Xml.Backups.BackupInst | ForEach-Object {
[PSCustomObject] @{
DisplayName = $_.GPODisplayName.'#cdata-section'
Domain = $_.GPODomain.'#cdata-section'
Guid = $_.GPOGUid.'#cdata-section'
DomainGuid = $_.GPODomainGuid.'#cdata-section'
DomainController = $_.GPODomainController.'#cdata-section'
BackupTime = $_.BackupTime.'#cdata-section'
ID = $_.ID.'#cdata-section'
Comment = $_.Comment.'#cdata-section'
}
}
$BackupInformation
} else {
Write-Warning "Get-GPOZaurrBackupInformation - No backup information available"
}
$BackupInformation
} else {
Write-Warning "Get-GPOZaurrBackupInformation - No backup information available"
}
}
End {
}
}
+102 -32
View File
@@ -7,41 +7,111 @@
[string[]] $ExcludeDomains,
[alias('Domain', 'Domains')][string[]] $IncludeDomains,
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath
[string[]] $GPOPath,
[string] $BackupPath,
[switch] $BackupDated
)
$GPOs = Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -GPOPath $GPOPath
#$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation
$Count = 0
$GPOSummary = foreach ($GPO in $GPOs) {
#$QueryServer = $ForestInformation['QueryServers'][$GPO.Domain]['HostName'][0]
if ($Type -eq 'Empty') {
if ($GPO.ComputerSettingsAvailable -eq $false -and $GPO.UserSettingsAvailable -eq $false) {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
Remove-GPO -Domain $GPO.Domain -Name $GPO.Name #-Server $QueryServer
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'EmptyAndUnlinked') {
if ($GPO.ComputerSettingsAvailable -eq $false -and $GPO.UserSettingsAvailable -eq $false -or $Gpo.Linked -eq $false) {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
Remove-GPO -Domain $GPO.Domain -Name $GPO.Name #-Server $QueryServer
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'Unlinked') {
if ($Gpo.Linked -eq $false) {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($GPO.Name) from $($GPO.Domain)"
$Count++
Remove-GPO -Domain $GPO.Domain -Name $GPO.Name #-Server $QueryServer
if ($LimitProcessing -eq $Count) {
break
Begin {
if ($BackupPath) {
$BackupRequired = $true
if ($BackupDated) {
$BackupFinalPath = "$BackupPath\$((Get-Date).ToString('yyyy-MM-dd_HH_mm_ss'))"
} else {
$BackupFinalPath = $BackupPath
}
Write-Verbose "Remove-GPOZaurr - Backing up to $BackupFinalPath"
$null = New-Item -ItemType Directory -Path $BackupFinalPath -Force
} else {
$BackupRequired = $false
}
$Count = 0
}
Process {
Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -GPOPath $GPOPath | ForEach-Object {
#$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation
#$GPOSummary = foreach ($GPO in $GPOs) {
#$QueryServer = $ForestInformation['QueryServers'][$_.Domain]['HostName'][0]
if ($Type -eq 'Empty') {
if ($_.ComputerSettingsAvailable -eq $false -and $_.UserSettingsAvailable -eq $false) {
if ($BackupRequired) {
try {
Write-Verbose "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$BackupInfo = Backup-GPO -Guid $_.Guid -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
if (($BackupRequired -and $BackupInfo) -or (-not $BackupRequired)) {
try {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain)"
Remove-GPO -Domain $_.Domain -Guid $_.Guid -ErrorAction Stop #-Server $QueryServer
} catch {
Write-Warning "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
$Count++
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'EmptyAndUnlinked') {
if ($_.ComputerSettingsAvailable -eq $false -and $_.UserSettingsAvailable -eq $false -or $_.Linked -eq $false) {
if ($BackupRequired) {
try {
Write-Verbose "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$BackupInfo = Backup-GPO -Guid $_.Guid -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
if (($BackupRequired -and $BackupInfo) -or (-not $BackupRequired)) {
try {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain)"
Remove-GPO -Domain $_.Domain -Guid $_.Guid -ErrorAction Stop #-Server $QueryServer
} catch {
Write-Warning "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
$Count++
if ($LimitProcessing -eq $Count) {
break
}
}
} elseif ($Type -eq 'Unlinked') {
if ($_.Linked -eq $false) {
if ($BackupRequired) {
try {
Write-Verbose "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain)"
$BackupInfo = Backup-GPO -Guid $_.Guid -Domain $_.Domain -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer
$BackupInfo
} catch {
Write-Warning "Remove-GPOZaurr - Backing up GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
if (($BackupRequired -and $BackupInfo) -or (-not $BackupRequired)) {
try {
Write-Verbose "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain)"
Remove-GPO -Domain $_.Domain -Guid $_.Guid -ErrorAction Stop #-Server $QueryServer
} catch {
Write-Warning "Remove-GPOZaurr - Removing GPO $($_.Name) from $($_.Domain) failed: $($_.Exception.Message)"
}
}
$Count++
if ($LimitProcessing -eq $Count) {
break
}
}
}
#}
#$GPOSummary
}
}
$GPOSummary
End {
}
}
+1 -1
View File
@@ -25,7 +25,7 @@
try {
Restore-GPO -Path $BackupFolder -BackupID $GPO.ID -Domain $GPO.Domain
} catch {
Write-Warning "Restore-GPOZaurr - Restoring GPO $($GPO.DisplayName) from $($_.Domain) failed: $($_.Exception.Message)"
Write-Warning "Restore-GPOZaurr - Restoring GPO $($GPO.DisplayName) from $($GPO.Domain) failed: $($_.Exception.Message)"
}
}
}