Import-GPO.ps1

Jan
24
2013

# Get-GPO.ps1
# Writen By Brandon Shell aka(BSonPosh)
# Gets GPO or GPOs according to Parameters
# Parameters
#  -Path:    Path to the backups
#  -GPOName: Name of the GPO
#  -Table:   Path of Migration Table file.
# Switches
#  -whatif:
#  -Verbose: Enables Verbose Logging
Param($path = $(throw ‘$path is required’),
$GPOName,
$Table,
[switch]$all,
[switch]$Verbose)

if($verbose){$verbosepreference = “continue”}else{$erroractionpreference = “SilentlyContinue”}
if(!($GPOName -or $All)){Write-Host “Please supply GPOName or -All to Import” -fore Yellow}
. “$pwd\Set-GPEnvironment.ps1″

Write-Host

# Testing for $PATH
Write-Verbose ” + Checking Backup Path $path”
if(!(test-Path $path)){return “Backup folder is invalid”}
Write-Verbose “   – Found Backup Path $path”

# Getting Backups to Import From
Write-Verbose ” + Getting Backups from $Path”
$backups = Get-GPOBackups $Path
Write-Verbose “   – Found [$($backups.count)] GPO Backups”

# Checking for Migration Table”
if($Table -and !(test-path $Table))
{
Write-Host “– Migration Table Path Invalid –” -fore Red
return
}
if($Table)
{
Write-Verbose ” – Using Migration Table $Table”
$GPMMigrationTable = $GPM.GetMigrationTable($Table)
}

$GPOi = @()

# Checking if GPOName was passed
if($GPOName)
{
Write-Verbose ” + Getting GPO Data for $GPOName”
$GPOi +=  $backups | where-Object{$_.GPODisplayName -eq $GPOName}
Write-Verbose “   – Found $($GPOi.GPODisplayName)”
}

if($all)
{
foreach($backup in $backups)
{
Write-Verbose ” + Adding $($backup.GPODisplayName)”
$GPOi += $backup
}
}

foreach($gpo in $GPOi)
{
# Create and GPO and Import Backup
Write-Verbose ” + Creating GPO to Import to”
$NewGPO = $gpmDomain.CreateGPO()
$NewGPO.DisplayName = “$($gpo.GPODisplayName)”
Write-Verbose “   – Created GPO $($NewGPO.Displayname) with GUID $($NewGPO.ID)”
if($Table)
{
Write-Verbose “   – Importing GPO [$($gpo.GPODisplayName)] with $Table”
#$NewGPO.Import(0,$gpo,$GPMMigrationTable) | out-Null
}
else
{
Write-Verbose “   – Importing GPO [$($gpo.GPODisplayName)]”
#$NewGPO.Import(0,$gpo) | out-Null
}
if($?)
{
Write-Host “Successfully Imported $($gpo.GPODisplayName)” -Fore Green
}
else
{
Write-Host “An error occurred while importing $($gpo.GPODisplayName).” -fore Red
Write-Host “Error: $($error[0])” -fore Red
}
}
Write-Host

Copyright © GPanswers.com. All rights reserved. GPanswers.com is a service of PolicyPak Software