Question:
I want to backup GPOs using their names instead of their ID’s. Currently I have:
1 2 3 4 5 6 7 8 |
$GPO_Temp_Backup = get-gpo -all -domain $domain Foreach ($GPO in $GPO_Temp_Backup) { $GPOBackup = Backup GPO -name $GPO.DisplayName -path ($Current_Drive + $GPO_Backup_Location) -Domain $domain; write-host $GPO.DisplayName } |
I can post any other parts of the script as needed but most of the missing variable values shouldn’t matter for the question. As of now, it still backs up the GPO’s as their ID’s
Answer:
So I decided to change the code a bit so I could debug what was going on. I added writeout $GPOBackup
and writeout $GPO
. Interestingly this is similar to what I saw. (Example taken from Microsoft page)
writeout $GPOBackup
provided me with:
1 2 3 4 5 6 7 8 9 10 |
C:\PS> Backup-Gpo -Name TestGPO -Path C:\GpoBackups -Comment "Weekly Backup" DisplayName : TestGPO GpoId : 35c12ab3-956c-45d5-973b-46b17d225f47 Id : 2b509d4e-40f5-4337-82f7-458584555d0c BackupDirectory : C:\GpoBackups CreationTime : 2/25/2009 8:48:19 PM DomainName : contoso.com Comment : Weekly Backup |
writeout $GPO
provided me with:
1 2 3 4 |
Id: 35c12ab3-956c-45d5-973b-46b17d225f47 DisplayName : TestGPO Path: cn={35c12ab3-956c-45d5-973b-46b17d225f47} |
As you can see in the first output there is a GpoId and an Id. The output of the second command suddenly changes the Id to what was originally the GpoID. When the
backup-gpo
command saves the GPO it saves it using the Id that you receive from the first block of code. The Id then changes to what is the GpoId
. I have no clue as to why this happens but at any rate, the fix for me was simple once I figured this out. The follow code allowed me to backup GPOs
as DisplayName
1 2 3 4 5 6 7 |
Foreach ($GPO in $GPO_Temp_Backup) { $GPOBackup = Backup-GPO -name $GPO.DisplayName -path ($Current_drive + $GPO_Backup_Location + "\" + "{" + $GPO.Id + "}") -domain $domain rename-item -path ($Current_drive + $GPO_Backup_Location + "\" + "{" + $GPOBackup.Id + "}") -newname $GPO.DisplayName } $oldfilename = ($Current_drive + $GPO_Backup_Location + "\" + "{" + $GPOBackup.Id + "}") write $oldfilename |