-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-GPOfromString.ps1
80 lines (73 loc) · 2.68 KB
/
Find-GPOfromString.ps1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#############################################################################
# Author : Tyler Cox
#
# Version : 1.0
# Created : 11/15/2021
# Modified :
#
# Purpose : This script will search GPOs for a specified string
#
# Requirements: A computer with Active Directory Admin Center (ADAC) installed and a
# user account with enough privileges
#
# Change Log: Ver 1.0 - Initial release
#
#############################################################################
Function Find-GPOfromString
{
[cmdletbinding()]
param(
[parameter(
Mandatory = $true,
ValueFromPipeline = $false)]
[string]$string, #String we want to search for
[parameter(
Mandatory = $false,
ValueFromPipeline = $false)]
[string]$DomainName = $env:USERDNSDOMAIN, #Get the domain we are searching in based off the user's current domain
[parameter(
Mandatory = $false,
ValueFromPipeline = $false)]
[string[]]$MatchedGPOList = @()
)
#Get all GPOs in the domain
write-host "Getting all the GPOs in $DomainName"
try
{
Import-Module grouppolicy -ErrorAction Stop #Import Group Policy Module
}
catch
{
Write-Host "ERROR! Cannot import Group Policy module! Please make sure ADAC is installed!" -ForegroundColor Red
Exit
}
try
{
$AllGPOs = Get-GPO -All -Domain $DomainName -ErrorAction Stop #Pull all GPOs
}
catch
{
Write-Host "ERROR! Cannot extract Group Policies from the domain! make sure you are running this with enough permissions!" -ForegroundColor Red
Exit
}
#Inspect each GPO's XML for the string
Write-Host "Beginning search.."
foreach ($gpo in $AllGPOs)
{
$report = Get-GPOReport -Guid $gpo.Id -ReportType Xml
if ($report -match $string)
{
write-host "Successful match in: $($gpo.DisplayName)" -foregroundcolor "Green"
$MatchedGPOList += "$($gpo.DisplayName)"
}
else
{
Write-Host "No match in: $($gpo.DisplayName)"
}
}
write-host "Results: "
foreach ($match in $MatchedGPOList)
{
write-host "Match found in: $($match)"
}
}