+49 8033 303222

Powershell – Backup GPOs by name

Beschreibung:

das Script legt – soweit nicht vorhanden – auf dem aktuellen Laufwerk das Verzeichnis \gpobackups an und sichert dort alle GPOs der Domain ( Variable $domain)

anschließend werden alle Verzeichnisse nach der Datei gpreport.xml durchsucht – aus dieser der GPO Name gelesen und die Ordner anschließend umbenannt …

 

$xmlnsGpSettings = „http://www.microsoft.com/GroupPolicy/Settings“
$xmlnsSchemaInstance = „http://www.w3.org/2001/XMLSchema-instance“
$xmlnsSchema = „http://www.w3.org/2001/XMLSchema“

$Current_drive = (get-location).Drive.Root
$Backup_Location = „gpobackups“
$domain = „nwtraders.com“

$complete_Backup_Path = join-path $Current_drive $Backup_Location
if (!(test-path $complete_Backup_Path))
{
New-Item -ItemType Directory -path $complete_Backup_Path
}

Backup-Gpo -All -Path $complete_Backup_Path -domain $domain
$files = Get-ChildItem -Path $complete_Backup_Path -recurse -Filter gpreport.xml -name

foreach ( $file in $files ) {
$file = Join-Path $complete_Backup_Path $file
[xml]$xmlDoc = get-content $file;
$xmlNameSpaceMgr = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable);
$xmlNameSpaceMgr.AddNamespace(„“, $xmlnsGpSettings);
$xmlNameSpaceMgr.AddNamespace(„gp“, $xmlnsGpSettings);
$xmlNameSpaceMgr.AddNamespace(„xsi“, $xmlnsSchemaInstance);
$xmlNameSpaceMgr.AddNamespace(„xsd“, $xmlnsSchema);

$GPOName =  $xmlDoc.DocumentElement.Name

$subdir = Split-Path $file
$newdir = join-path $complete_Backup_Path $GPOName
write-host „old Path: “ $subdir
write-host „new Path: “ $newdir
rename-item $subdir $GPOName
}