This commit is contained in:
Przemysław Kłys
2020-04-03 11:17:07 +02:00
parent 956d111911
commit 1241cf3680
11 changed files with 309 additions and 133 deletions
+81
View File
@@ -0,0 +1,81 @@
function Backup-GPOZaurr {
[cmdletBinding()]
param(
[int] $LimitProcessing,
[validateset('All', 'Empty', 'Unlinked', 'EmptyAndUnlinked')][string] $Type = 'All',
[alias('ForestName')][string] $Forest,
[string[]] $ExcludeDomains,
[alias('Domain', 'Domains')][string[]] $IncludeDomains,
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath,
[string] $BackupPath
)
# Logging Paths
$DateDirectory = "$BackupPath\$((Get-Date).ToString('yyyy-MM-dd_HH_mm_ss'))"
Write-Verbose "Backup-GPOZaurr - Backing up to $DateDirectory"
$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
}
+22 -18
View File
@@ -7,26 +7,30 @@
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath
)
if (-not $GPOPath) {
if (-not $ExtendedForestInformation) {
$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains
} else {
$ForestInformation = $ExtendedForestInformation
Begin {
if (-not $GPOPath) {
$ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation
}
foreach ($Domain in $ForestInformation.Domains) {
Get-GPO -All -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain | ForEach-Object {
$XMLContent = Get-GPOReport -ID $_.ID -ReportType XML -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain
Get-XMLGPO -XMLContent $XMLContent
}
Process {
if (-not $GPOPath) {
foreach ($Domain in $ForestInformation.Domains) {
Get-GPO -All -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain | ForEach-Object {
$XMLContent = Get-GPOReport -ID $_.ID -ReportType XML -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain
Get-XMLGPO -XMLContent $XMLContent
}
}
}
} else {
foreach ($Path in $GPOPath) {
Get-ChildItem -LiteralPath $Path -Recurse -Filter *.xml | ForEach-Object {
$XMLContent = [XML]::new()
$XMLContent.Load($_.FullName)
Get-XMLGPO -XMLContent $XMLContent
} else {
foreach ($Path in $GPOPath) {
Get-ChildItem -LiteralPath $Path -Recurse -Filter *.xml | ForEach-Object {
$XMLContent = [XML]::new()
$XMLContent.Load($_.FullName)
Get-XMLGPO -XMLContent $XMLContent
}
}
}
}
}
End {
}
}
+26
View File
@@ -0,0 +1,26 @@
function Get-GPOZaurrBackupInformation {
[cmdletBinding()]
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'
}
}
$BackupInformation
} else {
Write-Warning "Get-GPOZaurrBackupInformation - No backup information available"
}
}
}
+47
View File
@@ -0,0 +1,47 @@
function Remove-GPOZaurr {
[cmdletBinding(SupportsShouldProcess)]
param(
[parameter(Mandatory)][validateset('Empty', 'Unlinked', 'EmptyAndUnlinked')][string] $Type,
[int] $LimitProcessing,
[alias('ForestName')][string] $Forest,
[string[]] $ExcludeDomains,
[alias('Domain', 'Domains')][string[]] $IncludeDomains,
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath
)
$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
}
}
}
}
$GPOSummary
}
+64
View File
@@ -0,0 +1,64 @@
function Restore-GPOZaurr {
[cmdletBinding()]
param(
[parameter(Mandatory)][string] $BackupFolder,
[alias('Name')][string] $DisplayName,
[string] $NewDisplayName,
[string] $Domain,
[switch] $SkipBackupSummary
)
if ($BackupFolder) {
if (Test-Path -LiteralPath $BackupFolder) {
if ($DisplayName) {
if (-not $SkipBackupSummary) {
$BackupSummary = Get-GPOZaurrBackupInformation -BackupFolder $BackupFolder
if ($Domain) {
[Array] $FoundGPO = $BackupSummary | Where-Object { $_.DisplayName -eq $DisplayName -and $_.Domain -eq $Domain }
} else {
[Array] $FoundGPO = $BackupSummary | Where-Object { $_.DisplayName -eq $DisplayName }
}
foreach ($GPO in $FoundGPO) {
if ($NewDisplayName) {
Import-GPO -Path $BackupFolder -BackupID $GPO.ID -Domain $GPO.Domain -TargetName $NewDisplayName -CreateIfNeeded
} else {
Write-Verbose "Restore-GPOZaurr - Restoring GPO $($GPO.DisplayName) from $($GPO.Domain) / BackupId: $($GPO.ID)"
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)"
}
}
}
} else {
if ($Domain) {
Write-Verbose "Restore-GPOZaurr - Restoring GPO $($Name) from $($Domain)"
try {
Restore-GPO -Path $BackupFolder -Name $Name -Domain $Domain
} catch {
Write-Warning "Restore-GPOZaurr - Restoring GPO $($Name) from $($Domain) failed: $($_.Exception.Message)"
}
} else {
Write-Verbose "Restore-GPOZaurr - Restoring GPO $($Name)"
try {
Restore-GPO -Path $BackupFolder -Name $Name
} catch {
Write-Warning "Restore-GPOZaurr - Restoring GPO $($Name) failed: $($_.Exception.Message)"
}
}
}
} else {
$BackupSummary = Get-GPOZaurrBackupInformation -BackupFolder $BackupFolder
foreach ($GPO in $BackupSummary) {
Write-Verbose "Restore-GPOZaurr - Restoring GPO $($GPO.DisplayName) from $($GPO.Domain) / BackupId: $($GPO.ID)"
try {
Restore-GPO -Path $BackupFolder -Domain $GPO.Domain -BackupId $GPO.ID
} catch {
Write-Warning "Restore-GPOZaurr - Restoring GPO $($GPO.DisplayName) from $($GPO.Domain) failed: $($_.Exception.Message)"
}
}
}
} else {
Write-Warning "Restore-GPOZaurr - BackupFolder incorrect ($BackupFolder)"
}
}
}
+12 -1
View File
@@ -5,7 +5,8 @@
[string[]] $ExcludeDomains,
[alias('Domain', 'Domains')][string[]] $IncludeDomains,
[System.Collections.IDictionary] $ExtendedForestInformation,
[string[]] $GPOPath
[string[]] $GPOPath,
[switch] $DeleteExisting
)
if ($GPOPath) {
if (-not $ExtendedForestInformation) {
@@ -13,8 +14,18 @@
} else {
$ForestInformation = $ExtendedForestInformation
}
if ($DeleteExisting) {
$Test = Test-Path -LiteralPath $GPOPath
if ($Test) {
Write-Verbose "Save-GPOZaurrFiles - Removing existing content in $GPOPath"
Remove-Item -LiteralPath $GPOPath -Recurse
}
}
$null = New-Item -ItemType Directory -Path $GPOPath -Force
foreach ($Domain in $ForestInformation.Domains) {
Write-Verbose "Save-GPOZaurrFiles - Processing GPO for $Domain"
Get-GPO -All -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain | ForEach-Object {
$XMLContent = Get-GPOReport -ID $_.ID.Guid -ReportType XML -Server $ForestInformation.QueryServers[$Domain].HostName[0] -Domain $Domain
$Path = [io.path]::Combine($GPOPath, "$($_.ID.Guid).xml")