Skip to content

Instantly share code, notes, and snippets.

@karimelmel
Created September 11, 2017 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karimelmel/0d66464f33f38af9161dec26ef82172e to your computer and use it in GitHub Desktop.
Save karimelmel/0d66464f33f38af9161dec26ef82172e to your computer and use it in GitHub Desktop.
GPO ImportExport
#Gathers credentials for connection to VMs.
$VM1Creds = Get-Credential -UserName entmobsec.com\administrator -Message EntMobSec
$VM2Creds = Get-Credential -UserName prod.com\administrator -Message Prod
#EXPORT
#Creates a PSSession to DC in Lab environment.
$VM1session = New-PSSession -VMName "Server 2016" -Credential $VM1Creds
#Backup all GPOs starting with name "Test" in
$gpobackups = Invoke-Command -Session $VM1session -ScriptBlock {
$ShareCreds = Get-Credential -UserName administrator -Message "Prod Admin"
New-PSDrive -Name "X" -PSProvider "FileSystem" -Root "\\dc1\shared" -Persist -Credential $ShareCreds | out-null
#Backup all GPO's with name test. Search string can be changed.
Get-GPO -All -Domain $SrceDomain |
Where-Object {$_.DisplayName -like '*test*'} |
Select-Object DisplayName, Id | Backup-GPO -Path X:\ -Verbose
}
#IMPORT
#Creates a PSSession to DC in Production Environment
$VM2session = New-PSSession -VMName "Prod2016" -Credential $VM2Creds
#Imports all GPO Backups from Lab to Production environment.
#Removes TEST from the name and creates a new object (if it doesn't already exist, then it merges)
Invoke-Command -Session $VM2session -ScriptBlock {
foreach ($gpobackup in $using:gpobackups) {
$dn = $gpobackup.DisplayName
Import-GPO -BackupGpoName $dn -Path C:\Shared -TargetName $dn.substring(5) -CreateIfNeeded -Verbose
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment