New-GPOBackup.ps1

Jan
24
2013

# New-GPOBackup.ps1
# Writen By Brandon Shell (www.BSonPosh.com)
# Creates a New Backup for a GPO or ALL if none are specified
# Parameters
#  -Path: Path for the backup folder REQUIRED
#  -GPOName: Name of the GPO
# Switches
#  -Date: Creates a Subfolder Under Backup Path for the Date
#  -Verbose: Enables Verbose Logging
# Requires Set-GPEnvironment.ps1
Param($Path = $(throw ‘$Path is Require’),
$GPOName,
[switch]$date,
[switch]$Verbose)
if($verbose){$verbosepreference = “continue”}
. “$pwd\Set-GPEnvironment.ps1″
Write-Host
$GPOs = $gpmDomain.SearchGPOs($gpm.CreateSearchCriteria())

if($GPOName)
{
$GPOs = $GPOs | Where-Object{$_.DisplayName -eq $GPOName}
}

if($date)
{
$backupPath = “{0}\{1:yyyyMMdd}” -f $Path,(Get-Date)
if(!(test-Path $backupPath))
{
New-Item $backupPath -type Directory -force | out-Null
}
}
else
{
$backupPath = $path
if(!(test-Path $backupPath))
{
New-Item $backupPath -type Directory -force | out-Null
}
}

foreach($gpo in $gpos)
{
write-Host “Backing Up <$($GPO.DisplayName)> to <$backupPath>”
$gpo.Backup($backupPath,$null,$null) | out-null
}
Write-Host