From c5779cdae05b9ced21458dfa1d619c897d0be628 Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Sun, 6 Dec 2020 14:41:07 +0100 Subject: [PATCH] Update --- Private/Get-XMLGPO.ps1 | 32 +++++++++++++++++++--- Public/Backup-GPOZaurr.ps1 | 55 +++++++++----------------------------- Public/Get-GPOZaurr.ps1 | 13 ++++++--- 3 files changed, 49 insertions(+), 51 deletions(-) diff --git a/Private/Get-XMLGPO.ps1 b/Private/Get-XMLGPO.ps1 index 965c8c9..1e073d8 100644 --- a/Private/Get-XMLGPO.ps1 +++ b/Private/Get-XMLGPO.ps1 @@ -8,7 +8,8 @@ [System.Collections.IDictionary] $ADAdministrativeGroups, [string] $Splitter = [System.Environment]::NewLine, [switch] $ReturnObject, - [System.Collections.IDictionary] $ExcludeGroupPolicies + [System.Collections.IDictionary] $ExcludeGroupPolicies, + [string[]] $Type ) if ($XMLContent.GPO.LinksTo) { $LinkSplit = ([Array] $XMLContent.GPO.LinksTo).Where( { $_.Enabled -eq $true }, 'Split') @@ -170,7 +171,7 @@ } } if ($PermissionsOnly) { - [PsCustomObject] @{ + $GPOOutput = [PsCustomObject] @{ 'DisplayName' = $XMLContent.GPO.Name 'DomainName' = $XMLContent.GPO.Identifier.Domain.'#text' 'GUID' = $XMLContent.GPO.Identifier.Identifier.InnerText -replace '{' -replace '}' @@ -201,7 +202,7 @@ } } } elseif ($OwnerOnly) { - [PsCustomObject] @{ + $GPOOutput = [PsCustomObject] @{ 'DisplayName' = $XMLContent.GPO.Name 'DomainName' = $XMLContent.GPO.Identifier.Domain.'#text' 'GUID' = $XMLContent.GPO.Identifier.Identifier.InnerText -replace '{' -replace '}' @@ -212,7 +213,7 @@ 'GPODistinguishedName' = $GPO.Path } } else { - [PsCustomObject] @{ + $GPOOutput = [PsCustomObject] @{ 'DisplayName' = $XMLContent.GPO.Name 'DomainName' = $XMLContent.GPO.Identifier.Domain.'#text' 'GUID' = $XMLContent.GPO.Identifier.Identifier.InnerText -replace '{' -replace '}' @@ -297,4 +298,27 @@ 'GPOObject' = $GPO } } + if ($PermissionsOnly -or $OwnerOnly) { + $GPOOutput + } else { + if (-not $Type -or $Type -contains 'All') { + $GPOOutput + } else { + if ($Type -contains 'Empty') { + if ($GPOOutput.Empty -eq $true) { + $GPOOutput + } + } + if ($Type -contains 'Unlinked') { + if ($GPOOutput.Linked -eq $false) { + $GPOOutput + } + } + if ($Type -contains 'Disabled') { + if ($GPOOutput.Enabled -eq $false) { + $GPOOutput + } + } + } + } } \ No newline at end of file diff --git a/Public/Backup-GPOZaurr.ps1 b/Public/Backup-GPOZaurr.ps1 index 6f14e44..a0f64f1 100644 --- a/Public/Backup-GPOZaurr.ps1 +++ b/Public/Backup-GPOZaurr.ps1 @@ -44,7 +44,7 @@ [cmdletBinding(SupportsShouldProcess)] param( [int] $LimitProcessing, - [validateset('All', 'Empty', 'Unlinked')][string[]] $Type = 'All', + [validateset('Empty', 'Unlinked', 'Disabled', 'All')][string[]] $Type = 'All', [alias('ForestName')][string] $Forest, [string[]] $ExcludeDomains, [alias('Domain', 'Domains')][string[]] $IncludeDomains, @@ -63,49 +63,18 @@ $Count = 0 } Process { - Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation | ForEach-Object { - if ($Type -contains 'All') { - Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName)" - $Count++ - try { - $BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.DomainName -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer - $BackupInfo - } catch { - Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName) failed: $($_.Exception.Message)" - } - if ($LimitProcessing -eq $Count) { - break - } + Get-GPOZaurr -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -Type $Type | ForEach-Object { + $GPO = $_ + Write-Verbose "Backup-GPOZaurr - Backing up GPO $($GPO.DisplayName) from $($GPO.DomainName)" + $Count++ + try { + $BackupInfo = Backup-GPO -Guid $GPO.GUID -Domain $GPO.DomainName -Path $BackupFinalPath -ErrorAction Stop + $BackupInfo + } catch { + Write-Warning "Backup-GPOZaurr - Backing up GPO $($GPO.DisplayName) from $($GPO.DomainName) failed: $($_.Exception.Message)" } - if ($Type -notcontains 'All' -and $Type -contains 'Empty') { - if ($_.ComputerSettingsAvailable -eq $false -and $_.UserSettingsAvailable -eq $false) { - Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName)" - $Count++ - try { - $BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.DomainName -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer - $BackupInfo - } catch { - Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName) failed: $($_.Exception.Message)" - } - if ($LimitProcessing -eq $Count) { - break - } - } - } - if ($Type -notcontains 'All' -and $Type -contains 'Unlinked') { - if ($_.Linked -eq $false) { - Write-Verbose "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName)" - $Count++ - try { - $BackupInfo = Backup-GPO -Guid $_.GUID -Domain $_.DomainName -Path $BackupFinalPath -ErrorAction Stop #-Server $QueryServer - $BackupInfo - } catch { - Write-Warning "Backup-GPOZaurr - Backing up GPO $($_.DisplayName) from $($_.DomainName) failed: $($_.Exception.Message)" - } - if ($LimitProcessing -eq $Count) { - break - } - } + if ($LimitProcessing -eq $Count) { + break } } } diff --git a/Public/Get-GPOZaurr.ps1 b/Public/Get-GPOZaurr.ps1 index b325268..8b6cd9e 100644 --- a/Public/Get-GPOZaurr.ps1 +++ b/Public/Get-GPOZaurr.ps1 @@ -5,6 +5,8 @@ [string] $GPOName, [alias('GUID', 'GPOID')][string] $GPOGuid, + [validateset('Empty', 'Unlinked', 'Disabled', 'All')][string[]] $Type, + [alias('ForestName')][string] $Forest, [string[]] $ExcludeDomains, [alias('Domain', 'Domains')][string[]] $IncludeDomains, @@ -42,6 +44,9 @@ } } } + if ($OwnerOnly -or $PermissionsOnly -and $Type) { + Write-Warning "Get-GPOZaurr - Using PermissionOnly or OwnerOnly with Type is not supported. " + } } Process { if (-not $GPOPath) { @@ -61,7 +66,7 @@ Write-Warning "Get-GPOZaurr - Failed to get GPOReport: $($_.Exception.Message). Skipping." continue } - Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO + Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO -Type $Type } else { $_ } @@ -79,7 +84,7 @@ Write-Warning "Get-GPOZaurr - Failed to get GPOReport: $($_.Exception.Message). Skipping." continue } - Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO + Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO -Type $Type } else { $_ } @@ -97,7 +102,7 @@ Write-Warning "Get-GPOZaurr - Failed to get GPOReport: $($_.Exception.Message). Skipping." continue } - Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO + Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -GPO $_ -PermissionsOnly:$PermissionsOnly.IsPresent -ADAdministrativeGroups $ADAdministrativeGroups -ReturnObject:$ReturnObject.IsPresent -ExcludeGroupPolicies $ExcludeGPO -Type $Type } else { $_ } @@ -110,7 +115,7 @@ Get-ChildItem -LiteralPath $Path -Recurse -Filter *.xml | ForEach-Object { $XMLContent = [XML]::new() $XMLContent.Load($_.FullName) - Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -PermissionsOnly:$PermissionsOnly.IsPresent -ExcludeGroupPolicies $ExcludeGPO + Get-XMLGPO -OwnerOnly:$OwnerOnly.IsPresent -XMLContent $XMLContent -PermissionsOnly:$PermissionsOnly.IsPresent -ExcludeGroupPolicies $ExcludeGPO -Type $Type } Write-Verbose "Get-GPOZaurr - Finished GPO content from XML files" }