162 lines
5.4 KiB
PowerShell
162 lines
5.4 KiB
PowerShell
##
|
|
## KISGPOExport.ps1 - Export GPOs for an OU to file
|
|
##
|
|
## Created by Charlie Silverman - Keep IT Simple
|
|
##
|
|
## Version History:
|
|
## 1.1.0 - Added history
|
|
## 1.5.0 - Changed to put exported GPOs into a subfolder based on the OU name.
|
|
## 2.0.0 - Added functionality to process all parent OUs. Added help function.
|
|
|
|
param (
|
|
[switch]$Help = $false,
|
|
[switch]$Parents = $false
|
|
)
|
|
|
|
function prettyPrintOU($ou) {
|
|
$myparts = $ou.split("/")
|
|
$result = ""
|
|
for ($counter = $myparts.length - 1; $counter -gt 0; --$counter) {
|
|
$result += "OU=" + $myparts[$counter] + ","
|
|
}
|
|
$domainparts = $myparts[0].split(".")
|
|
for ($counter = 0; $counter -lt $domainparts.length; ++$counter) {
|
|
$result += "DC=" + $domainparts[$counter]
|
|
if ($counter -lt $domainparts.length - 1) { $result += "," }
|
|
}
|
|
return $result
|
|
}
|
|
|
|
$ScriptName = $MyInvocation.MyCommand.Path
|
|
$ScriptDate = Get-Item $ScriptName | select LastWriteTime
|
|
$ScriptDate = $ScriptDate.LastWriteTime
|
|
$ScriptVersion = "2.0.0"
|
|
|
|
Write-Host "`r`n"
|
|
Write-Host "--------------------------------------------------------------" -ForegroundColor Gray
|
|
Write-Host " KISGPOExport.ps1" -ForegroundColor Green -NoNewLine
|
|
Write-Host " - Group Policy Export Script" -ForegroundColor White
|
|
Write-Host " Version $ScriptVersion - $ScriptDate" -ForegroundColor White
|
|
Write-Host " Written by" -ForegroundColor White -NoNewLine
|
|
Write-Host " Charlie Silverman - Keep IT Simple" -ForegroundColor Green
|
|
Write-Host "--------------------------------------------------------------" -ForegroundColor Gray
|
|
if ($Help) {
|
|
Write-Host
|
|
Write-Host "Purpose: Export Group Policy Objects to HTML for a given OU."
|
|
Write-Host "Command Syntax:"
|
|
Write-Host " KISGPOExport.ps1 (-parents) (-help)`n" -foregroundcolor green
|
|
Write-Host "Command Line Options:"
|
|
Write-Host
|
|
Write-Host -NoNewLine " -parents " -foregroundcolor Green
|
|
Write-Host "- Export GPOs for all parent OUs as well."
|
|
Write-Host -NoNewLine " -help " -foregroundcolor Green
|
|
Write-Host "- Display command help."
|
|
}
|
|
|
|
$OUList = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Select-Object CanonicalName | Sort-Object -Property CanonicalName
|
|
$prev = ""
|
|
$depth = 1
|
|
$domainname = $OUList[0].CanonicalName.split("/")[0]
|
|
$lastpos = 5
|
|
$toppos = 1
|
|
$selected = 0
|
|
$displaypage = 25
|
|
$datestamp = Get-Date -Format FileDate
|
|
#$bottompos = $toppos + 19
|
|
while ($selected -eq 0) {
|
|
Write-Host "`nSelect an OU to scan for linked GPOs:`n"
|
|
Write-Host "Domain: $domainname"
|
|
For ($x = $toppos-1; $x -lt $toppos + $displaypage - 1; $x++) {
|
|
if ($x -ge $OUList.length) { break }
|
|
$thisOU = $OUList[$x].CanonicalName
|
|
$OUParts = $thisOU.split("/");
|
|
$spacing = " |" * ($OUParts.length - 2)
|
|
$spacing = "$spacing |__"
|
|
if ($depth -lt $OUParts.length - 1) {
|
|
$spacing = " |" * ($OUParts.length - 2)
|
|
$spacing += " |__"
|
|
}
|
|
$position = ([string]($x+1)).PadLeft(3) + ":"
|
|
Write-Host $position -NoNewLine -ForegroundColor Yellow
|
|
Write-Host "$spacing $($OUParts[$OUParts.length - 1])"
|
|
# for ($y = 1; $y -lt $OUParts.length; $y++ {
|
|
$depth = $OUParts.length
|
|
}
|
|
$selected = Read-Host -Prompt 'Select line number or P for Previous page, N for next page, or Q to quit [N]'
|
|
switch($selected.toLower()) {
|
|
"n" {
|
|
if ($toppos + $displaypage -lt $OUList.length) {
|
|
$toppos += $displaypage
|
|
if ($toppos -gt $OUList.length) { $toppos = $OUList.length - 1 }
|
|
}
|
|
$selected = 0
|
|
}
|
|
"p" {
|
|
$toppos -= $displaypage
|
|
if ($toppos -lt 1) { $toppos = 1 }
|
|
$selected=0
|
|
}
|
|
"q" {
|
|
exit
|
|
}
|
|
}
|
|
try {
|
|
$selected = [int]$selected
|
|
} catch {
|
|
$selected = 0
|
|
}
|
|
}
|
|
|
|
$selectedOU = prettyPrintOU($OUList[$selected-1].CanonicalName)
|
|
While (([regex]::Matches($selectedOU, "," )).count -gt 1) {
|
|
Write-Host "`n`nProcessing GPOs in $selectedOU`n"
|
|
$myou = Get-ADOrganizationalunit $selectedOU
|
|
$CNParts = $selectedOU.split(",")
|
|
$folderpath = "";
|
|
for ($x = $CNParts.length - 1; $x -ge 0; --$x) {
|
|
$folderpath = "$folderpath\$($CNParts[$x])"
|
|
}
|
|
if (-not (Test-Path -Path "$pwd$folderpath")) {
|
|
$result = New-Item -ItemType Directory -Path "$pwd$folderpath"
|
|
}
|
|
$gpolist = $myou | Select-Object -ExpandProperty LinkedGroupPolicyObjects
|
|
$x = 1
|
|
if ($gpolist -is [string]) {
|
|
$gpoguid = $gpolist.split("}")[0].split("{")[1]
|
|
try {
|
|
$thisgpo = Get-GPO -Guid $gpoguid
|
|
Write-Host "Exporting GPO " -NoNewLine -ForegroundColor DarkGray
|
|
Write-Host $thisgpo.DisplayName -NoNewLine
|
|
Write-Host "..." -NoNewLine -ForegroundColor DarkGray
|
|
Get-GPOReport -Guid $gpoguid -ReportType 'HTML' -Path "$pwd\$domainname - $($thisgpo.DisplayName) - 20230530.html"
|
|
Write-Host " Complete" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "ERROR PROCESSING"
|
|
}
|
|
} else {
|
|
Write-Host "There are a total of $($gpolist.length) GPOs linked to $selectedOU"
|
|
for ($x = 0; $x -lt $gpolist.length; ++$x) {
|
|
$thisgpo = [string]$gpolist[$x]
|
|
$gpoguid = $thisgpo.split("}")[0].split("{")[1]
|
|
try {
|
|
$thisgpo = Get-GPO -Guid $gpoguid
|
|
Write-Host "Exporting GPO " -NoNewLine -ForegroundColor DarkGray
|
|
Write-Host $thisgpo.DisplayName -NoNewLine
|
|
Write-Host "..." -NoNewLine -ForegroundColor DarkGray
|
|
|
|
Get-GPOReport -Guid $gpoguid -ReportType 'HTML' -Path "$pwd$folderpath\$domainname - $($thisgpo.DisplayName) - $datestamp.html"
|
|
Write-Host " Complete" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "ERROR PROCESSING"
|
|
}
|
|
}
|
|
}
|
|
if ($Parents) {
|
|
$selectedOU = ($myou.distinguishedname -split ',', 2)[1]
|
|
} else {
|
|
$selectedOU = ""
|
|
}
|
|
}
|
|
Write-Host "`nOperation complete."
|
|
Write-Host "$x policies exported to HTML and stored in $pwd$folderpath"
|