-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-GpoByOu.ps1
More file actions
48 lines (40 loc) · 1.05 KB
/
Get-GpoByOu.ps1
File metadata and controls
48 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#Requires -Module ActiveDirectory
param (
[Parameter(Mandatory)]
[string]
$SearchBaseDn
)
$OuTree = Get-ADOrganizationalUnit -Filter * -SearchBase $SearchBaseDn -SearchScope Subtree
$AllGPOs = Get-GPO -All
# Functions
function GetGpoGuids {
param (
$LinkedGpos
)
$RegexPattern = '[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}'
foreach ($ou in $LinkedGpos) {
($ou | Select-String $RegexPattern).Matches.Value
}
}
function GetGpoFromGuid {
param (
$GpoGuid
,
$GpoSearchBank
)
$GpoSearchBank.Where({$_.Id -eq $GpoGuid})
}
# Main Script
foreach ($ou in $OuTree) {
$LinkedGpos = $ou.LinkedGroupPolicyObjects
$GpoGuids = GetGpoGuids -LinkedGpos $LinkedGpos
$Gpos = $GpoGuids | ForEach-Object {GetGpoFromGuid -GpoGuid $_ -GpoSearchBank $AllGPOs}
foreach ($gpo in $Gpos) {
$Result = [PSCustomObject]@{
OuName = $ou.Name
OuDN = $ou.DistinguishedName
GpoName = $gpo.DisplayName
}
$Result
}
}