Restore-GPO.ps1

# 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

Attachments:
 Restore-GPO.txt- Restore-GPO.ps1 script referenced in Chapter 11. Please change file extension from .txt to .ps1.
 

Be a hero! Report a problem with this web page here

Event Calendar

Find out where and when Jeremy will be speaking next.

November 2008
SMTWTFS
1
2345678
9101112131415
16171819202122
23242526272829
30
Mon, Nov 10 - Thu, Nov 13
Windows Connections -- Vegas
It's WinConnections time! All sorts of great speeches this time. GP Troubleshooting, My Pre-Con, App-V 101. Will you be there?
www.winconnections.com
Mon, Nov 17 - Tue, Nov 18
Two-Day Intensive Group Policy Essentials Workshop (taught by Jeremy Moskowitz (Lead Instructor) - GPanswers.com / Moskowitz, Inc. )
 Final class of 2008.
This is a Two-Day Intensive Group Policy Essentials Workshop in Atlanta, GA !
Wed, Nov 19 - Thu, Nov 20
Two-Day "Group Policy 2.0" Catch-Up Workshop (taught by Jeremy Moskowitz (Lead Instructor) - GPanswers.com / Moskowitz, Inc. )
 Final class of 2008.
This is a Two-Day "Group Policy 2.0" Catch-Up Workshop in Atlanta, GA !
Fri, Nov 21 - Fri, Nov 21
One-Day Advanced Group Policy Workshop (XP/Vista Focused) (taught by Jeremy Moskowitz (Lead Instructor) - GPanswers.com / Moskowitz, Inc. )
 Final class of 2008.
This is a One-Day Advanced Group Policy Workshop (XP/Vista Focused) in Atlanta, GA !