Jump to content

Recommended Posts

Posted

######## Saves output to E:\Local\GPOReports\GPOs_With_DCs.txt this folder structure and file should be created before running. ##########

# Vars

 

$OutFile = "E:\Local\GPOReports\GPOs_With_DCs.txt"

$Date = Get-date

 

# Get the string we want to search for

 

$string = Read-Host -Prompt "String to Search"

 

# Set the domain to search for GPOs

$DomainName = $env:USERDNSDOMAIN

 

# Find all GPOs in the current domain

write-host "Searching for GPOs in $DomainName"

 

Import-Module grouppolicy

$allGposInDomain = Get-GPO -All -Domain $DomainName

 

Add-Content -Path $OutFile -Value "############### $Date ###############"

Add-Content -Path $OutFile -Value "############### $string #####################"

Add-Content -Path $OutFile -Value "###################################################"

 

# create XML report and for each GPO and then search

 

Write-Host "Starting search...."

 

foreach ($gpo in $allGposInDomain) {

$report = Get-GPOReport -Guid $gpo.Id -ReportType Xml

if ($report -match $string) {

write-host -fore Magenta "---> Match in: $($gpo.DisplayName) <---"

Add-Content -Path $OutFile -Value $($gpo.DisplayName)

}

else {

Write-Host -fore Green "No in: $($gpo.DisplayName)"

}

}

# End

 

 

The above will let you search all the GPO's in a domain for a string, it outputs to a file **Make sure you change the path! maybe some use if you do not have access to AGPM. It needs the group policy module.

Posted (edited)

As I am lazy I do not often spend time functionalising my scripts but this quite often gets me into trouble! so here it is wrapped up in a function for your delectation :-

 

function Find-StringInGPO
{
 <#
     .SYNOPSIS
     Finds a string in the GPOs of a Domain - Created by Harry Plumtree.
     .DESCRIPTION
     Gets all the GPOs in a Domain and searches for the supplied string, default output location is set to c:\temp\
     .EXAMPLE
     Find-StringInGPO -SearchString DC1
     explains how to use the command
     can be multiple lines
     .EXAMPLE
     Find-StringInGPO
     another example
     can have as many examples as you like
 #>
 param
 (
   [Parameter(Position=1)]
   [string]
   $OutFile = "c:\temp\GPOsWithString.csv",
   
   [Parameter(Mandatory=$true, Position=0)]
   [string]$SearchString
 )
 # Vars
 $Date = Get-date
 
 # Set the domain to search for GPOs 
 $DomainName = $env:USERDNSDOMAIN 
 
 # Find all GPOs in the current domain 
 Write-Output -InputObject ('Finding all the GPOs in {0}' -f $DomainName) 
 Import-Module -Name grouppolicy 
 $allGposInDomain = Get-GPO -All -Domain $DomainName
 
 Add-Content -Path $OutFile -Value ('############### {0} ###############' -f $Date)
 Add-Content -Path $OutFile -Value ('############### {0} #####################' -f $SearchString)
 Add-Content -Path $OutFile -Value '###################################################'
 
 # Look through each GPO's XML for the string 
 Write-Output 'Starting search....' 
 foreach ($gpo in $allGposInDomain) { 
   $report = Get-GPOReport -Guid $gpo.Id -ReportType Xml 
   if ($report -match $SearchString) { 
     Write-Output ('---> Match found in: {0} <---' -f $gpo.DisplayName)
     Add-Content -Path $OutFile -Value $($gpo.DisplayName) 
   } 
   else { 
     Write-Output ('No match in: {0}' -f $gpo.DisplayName) 
   }
 } 
}

 

Now the only reason I have got this far, if I am honest is by using isesteroids from here http://www.powertheshell.com/isesteroids/ I am sure that I have mentioned this useful little utility in the past but the more I use it the more I like it, and is a must for anyone writing PowerShell.

Edited by HPlum78

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...