Restore-GPO.ps1

Jan
24
2013

# Restore-GPO.ps1
# Writen By Brandon Shell aka(BSonPosh)
# Gets GPO or GPOs according to Parameters
# Parameters
#  -GPOName: Name of the GPO
#  -Path: Path to Backup
# Switches
#  -all: Restores all GPOs in backup
#  -List: List All GPO’s
#  -whatif: Explains what would happen
#  -Verbose: Enables Verbose Logging
Param(
$GPOName,
$Path = $(throw ‘$Path is Required, Please Enter Path to Backups’),
[switch]$all,
[switch]$List,
[switch]$whatif,
[switch]$Verbose)

if($verbose){$verbosepreference = “continue”}else{$erroractionpreference = “SilentlyConintue”}
. “$pwd\Set-GPEnvironment.ps1″
Write-Host
Write-Verbose ” – Getting GPO backups from $Path”
$backups = Get-GPOBackups $path

if($List)
{
Write-Verbose ” – Listing GPOs from backup”
$backups | Select-Object GPODisplayName,ID,Timestamp,Comment | Format-Table -auto
return
}

if($GPOName)
{
Write-Verbose ” – Restoring $GPOName”
$gpo = $backups | Where-Object{$_.GPODisplayName -eq $GPOName}
if($whatif){Write-Host “What if: Performing operation `”Restore`” on Target `”$($GPO.GPODisplayName)`”"}
else{$gpmDomain.RestoreGPO($GPO,0) | out-Null}
if($?){Write-Host “Restore of $($GPO.GPODisplayName) Complete” -fore Green}
else{Write-Host “Restore of $($GPO.GPODisplayName) encountered Error $($error[0])” -fore Red}
}
if($all)
{
Write-Verbose ” + Restoring All GPOs from Backup”
foreach($gpo in $backups)
{
Write-Verbose “   + Found GPO $($gpo.GPODisplayName)”
Write-Verbose “     – Restoring GPO”
if($whatif){Write-Host “What if: Performing operation `”Restore`” on Target `”$($GPO.GPODisplayName)`”"}
else{$gpmDomain.RestoreGPO($GPO,0) | out-Null}
if($?){Write-Host “Restore of $($GPO.GPODisplayName) Complete” -fore Green}
else{Write-Host “Restore of $($GPO.GPODisplayName) encountered Error $($error[0])” -fore Red}
Write-Verbose “     – Restored GPO”
}
}
Write-Host