From 3fce54e75aac6c0d562e37fad38730bec6fe9f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20K=C5=82ys?= Date: Tue, 7 Apr 2020 20:26:28 +0200 Subject: [PATCH] Update --- Examples/Example-01-BackupGPOs.ps1 | 4 +- .../Example-02-RemoveEmptyAndUnlinkedGPOs.ps1 | 11 +- Public/Backup-GPOZaurr.ps1 | 145 ++++++++++-------- Public/Get-GPOZaurr.ps1 | 1 + Public/Get-GPOZaurrBackupInformation.ps1 | 40 +++-- Public/Remove-GPOZaurr.ps1 | 134 ++++++++++++---- Public/Restore-GPOZaurr.ps1 | 2 +- 7 files changed, 210 insertions(+), 127 deletions(-) diff --git a/Examples/Example-01-BackupGPOs.ps1 b/Examples/Example-01-BackupGPOs.ps1 index e9d283d..eb0fe97 100644 --- a/Examples/Example-01-BackupGPOs.ps1 +++ b/Examples/Example-01-BackupGPOs.ps1 @@ -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 \ No newline at end of file +Get-GPOZaurrBackupInformation -BackupFolder $GPOSummary[0].BackupDirectory | Format-Table -a \ No newline at end of file diff --git a/Examples/Example-02-RemoveEmptyAndUnlinkedGPOs.ps1 b/Examples/Example-02-RemoveEmptyAndUnlinkedGPOs.ps1 index a710ebb..5cb27e3 100644 --- a/Examples/Example-02-RemoveEmptyAndUnlinkedGPOs.ps1 +++ b/Examples/Example-02-RemoveEmptyAndUnlinkedGPOs.ps1 @@ -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 \ No newline at end of file +$BackupPath = "$Env:UserProfile\Desktop\GPO" +Remove-GPOZaurr -Type EmptyAndUnlinked -BackupPath $BackupPath -BackupDated -LimitProcessing 2 -Verbose \ No newline at end of file diff --git a/Public/Backup-GPOZaurr.ps1 b/Public/Backup-GPOZaurr.ps1 index 39c3d4b..50b726f 100644 --- a/Public/Backup-GPOZaurr.ps1 +++ b/Public/Backup-GPOZaurr.ps1 @@ -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 { + + } } \ No newline at end of file diff --git a/Public/Get-GPOZaurr.ps1 b/Public/Get-GPOZaurr.ps1 index 29e6269..3a9d4da 100644 --- a/Public/Get-GPOZaurr.ps1 +++ b/Public/Get-GPOZaurr.ps1 @@ -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 } diff --git a/Public/Get-GPOZaurrBackupInformation.ps1 b/Public/Get-GPOZaurrBackupInformation.ps1 index 62f43f1..571170e 100644 --- a/Public/Get-GPOZaurrBackupInformation.ps1 +++ b/Public/Get-GPOZaurrBackupInformation.ps1 @@ -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 { + + } } \ No newline at end of file diff --git a/Public/Remove-GPOZaurr.ps1 b/Public/Remove-GPOZaurr.ps1 index d440d99..613df50 100644 --- a/Public/Remove-GPOZaurr.ps1 +++ b/Public/Remove-GPOZaurr.ps1 @@ -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 { + + } } \ No newline at end of file diff --git a/Public/Restore-GPOZaurr.ps1 b/Public/Restore-GPOZaurr.ps1 index c03391d..c34e939 100644 --- a/Public/Restore-GPOZaurr.ps1 +++ b/Public/Restore-GPOZaurr.ps1 @@ -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)" } } }