From cab639239fd7610d4338b08b9cf1792c9eaca155 Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Sun, 5 Dec 2021 11:01:19 +0100 Subject: [PATCH] Fixes #30 --- .../Example-51-FindEmptyOUsWithGPOLinks.ps1 | 4 +- Private/Get-WellKnownFolders.ps1 | 40 +++++++++++++++++++ Private/Invoke.GPOZaurrOrganizationalUnit.ps1 | 3 +- Public/Get-GPOZaurrOrganizationalUnit.ps1 | 13 ++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 Private/Get-WellKnownFolders.ps1 diff --git a/Examples/Example-51-FindEmptyOUsWithGPOLinks.ps1 b/Examples/Example-51-FindEmptyOUsWithGPOLinks.ps1 index 611412f..789e418 100644 --- a/Examples/Example-51-FindEmptyOUsWithGPOLinks.ps1 +++ b/Examples/Example-51-FindEmptyOUsWithGPOLinks.ps1 @@ -1,6 +1,5 @@ Import-Module "$PSScriptRoot\..\GPoZaurr.psd1" -Force - $OUs = Get-GPOZaurrOrganizationalUnit $Ous | Format-Table @@ -13,5 +12,6 @@ Get-GPOZaurrOrganizationalUnit -Verbose -ExcludeOrganizationalUnit @( ) | Format-Table Invoke-GPOZaurr -Type GPOOrganizationalUnit -Online -FilePath $PSScriptRoot\Reports\GPOZaurrOU.html -Exclusions @( - #'*OU=Production,DC=ad,DC=evotec,DC=pl' + '*OU=Production,DC=ad,DC=evotec,DC=pl' + '*OU=Accounts,OU=Administration,DC=ad,DC=evotec,DC=xyz' ) \ No newline at end of file diff --git a/Private/Get-WellKnownFolders.ps1 b/Private/Get-WellKnownFolders.ps1 new file mode 100644 index 0000000..d502cad --- /dev/null +++ b/Private/Get-WellKnownFolders.ps1 @@ -0,0 +1,40 @@ +function Get-WellKnownFolders { + <# + .SYNOPSIS + Gets users and computers well known folders for a forest + + .DESCRIPTION + Gets users and computers well known folders for a forest + + .PARAMETER Forest + Target different Forest, by default current forest is used + + .PARAMETER ExcludeDomains + Exclude domain from search, by default whole forest is scanned + + .PARAMETER IncludeDomains + Include only specific domains, by default whole forest is scanned + + .PARAMETER ExtendedForestInformation + Ability to provide Forest Information from another command to speed up processing + + .EXAMPLE + Get-WellKnownFolders + + .NOTES + General notes + #> + [cmdletBinding()] + param( + [alias('ForestName')][string] $Forest, + [string[]] $ExcludeDomains, + [alias('Domain', 'Domains')][string[]] $IncludeDomains, + [System.Collections.IDictionary] $ExtendedForestInformation + ) + + $ForestInformation = Get-WinADForestDetails -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation -Extended + foreach ($Domain in $ForestInformation.Domains) { + $ForestInformation.DomainsExtended[$Domain].ComputersContainer + $ForestInformation.DomainsExtended[$Domain].UsersContainer + } +} \ No newline at end of file diff --git a/Private/Invoke.GPOZaurrOrganizationalUnit.ps1 b/Private/Invoke.GPOZaurrOrganizationalUnit.ps1 index d844662..3ae5f19 100644 --- a/Private/Invoke.GPOZaurrOrganizationalUnit.ps1 +++ b/Private/Invoke.GPOZaurrOrganizationalUnit.ps1 @@ -32,7 +32,7 @@ $Script:Reporting['GPOOrganizationalUnit']['Variables']['DeleteOU']++ #$Script:Reporting['GPOOrganizationalUnit']['Variables']['WillFix']++ #$Script:Reporting['GPOOrganizationalUnit']['Variables']['WillFixPerDomain'][$OU.DomainName]++ - } elseif ($OU.Status -contains 'Excluded') { + } elseif ($OU.Status -contains 'Excluded' -or $OU.Status -contains 'Excluded, Default OU') { $Script:Reporting['GPOOrganizationalUnit']['Variables']['Excluded']++ $Script:Reporting['GPOOrganizationalUnit']['Variables']['ExcludedOU'].Add($OU.Organizationalunit) } else { @@ -139,6 +139,7 @@ New-HTMLTableCondition -Name 'Status' -ComparisonType string -Value 'Delete OU' -BackgroundColor Red -Row New-HTMLTableCondition -Name 'Status' -ComparisonType string -Value 'OK' -BackgroundColor LightGreen -Row New-HTMLTableCondition -Name 'Status' -ComparisonType string -Value 'Excluded' -BackgroundColor DeepSkyBlue -Row + New-HTMLTableCondition -Name 'Status' -ComparisonType string -Value 'Excluded, Default OU' -BackgroundColor DeepSkyBlue -Row } -PagingOptions 10, 20, 30, 40, 50 -ExcludeProperty GPO } if ($Script:Reporting['Settings']['HideSteps'] -eq $false) { diff --git a/Public/Get-GPOZaurrOrganizationalUnit.ps1 b/Public/Get-GPOZaurrOrganizationalUnit.ps1 index aa91729..f3230a4 100644 --- a/Public/Get-GPOZaurrOrganizationalUnit.ps1 +++ b/Public/Get-GPOZaurrOrganizationalUnit.ps1 @@ -13,6 +13,7 @@ $CachedGPO = [ordered] @{} $ForestInformation = Get-WinADForestDetails -Extended -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation + $DefaultFolders = Get-WellKnownFolders -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation $GroupPolicies = Get-GPOZaurrAD -Forest $Forest -IncludeDomains $IncludeDomains -ExcludeDomains $ExcludeDomains -ExtendedForestInformation $ExtendedForestInformation foreach ($GPO in $GroupPolicies) { $CachedGPO[$GPO.GPODistinguishedName] = $GPO @@ -117,6 +118,12 @@ } } } + foreach ($Exclude in $DefaultFolders) { + if ($OU -eq "$Exclude") { + $Found = $false + break + } + } if (-not $Found) { continue } @@ -129,6 +136,12 @@ } } } + foreach ($Exclude in $DefaultFolders) { + if ($OU -eq "$Exclude") { + $Status = 'Excluded, Default OU' + break + } + } } [PSCustomObject] @{