# Backup GPO by displayname and not ID - powershell

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

• Author
Posts
• #6075

by JMeterX at 2013-04-17 09:57:30

I want to backup GPOs using their names instead of their ID's. Currently I have:

$GPO_Temp_Backup = get-gpo -all -domain$domainForeach ($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

by DonJ at 2013-04-17 10:28:15

I'm assuming those backticks are being used to let you break the command across multiple lines in your script; as an aside that's considered a bit of a poor practice. Definitely impedes the reading ;).

So... when you say "backs up GPOs as their IDs" are you referring to the filename that it is writing out?

by JMeterX at 2013-04-17 10:42:28

Correct, currently as it stands, it parses through the information and saves each individual GPO as it's unique Id property. I want for it to save them as its DisplayName property instead. Yes the backticks were to allow me to bring the command across multiple lines. Just out of curiosity why is this poor practice? In general I just find it easier to read when I am attempting to get something to work.

by DonJ at 2013-04-17 10:53:37

On the backtick thing, the character itself is hard to read. There are ways to break a line without using it. For example:

$params = @{Name=$GPO.DisplayName; Path=($Current_Drive +$GPO_Backup_location); Domain=$domain}Backup-GPO @params I agree with making scripts easy to read; there's just a general dislike of using backtick as a line-continuation character because it's a tricky little bugger to see. You can legally break a line after a comma, semicolon, or pipe character without using a backtick, so most of us try to arrage our line breaks that way if possible. The command doesn't appear to accept an alternative filename, which means you can't do it. The best you could probably do is build a list of GPO names and IDs, and after backing them up, rename the files to the way you want them. You could probably do that for each GPO inside your ForEach loop. by JMeterX at 2013-04-17 11:06:34 Rather new to powershell still, how would I go about doing that within the same foreach loop? I would still have to have a way for the variable to understand which DisplayName goes to what Id... Thanks for the info on the tick marks I was not aware of that. I thought a semicolon ended the entire code block. by DonJ at 2013-04-17 11:15:29 $GPO_Temp_Backup = get-gpo -all -domain $domainForeach ($GPO in $GPO_Temp_Backup) {$id = # get the GPO ID here $GPOBackup = Backup-GPO -name$GPO.DisplayName -path ($Current_Drive +$GPO_Backup_Location) -Domain $domain Rename-Item # use your$GPO.DisplayName and $id to rename the item write-host$GPO.DisplayName}

We don't use semicolons the same as C languages. It's not an end-of-line terminator.
[/code]

by JMeterX at 2013-04-17 11:21:51

Just as you sent that I attempted the rename-item function but it doesn't seem to find any of the folder names.

$GPO_Temp_Backup = get-gpo -all -domain$domain

Foreach ($GPO in$GPO_Temp_Backup) {$id =$GPOBackup = Backup-GPO -name $GPO.DisplayName -path ($Current_Drive + $GPO_Backup_Location) -domain$domainrename-item -path ($Current_drive +$GPO_Backup_Location + $GPO.Id) -newname$GPO.DisplayName}

This seemed to work but it says the ID's do not exist to be renamed

by DonJ at 2013-04-17 11:28:11

Well, your syntax looks correct. Check and make sure it does exist.

$oldfilename = ($Current_drive + $GPO_Backup_Location +$GPO.Id)Write $oldfilename Make sure that contains what you think it does, and that it matches the actual filename. Concatenating paths can be tricky; I often prefer to use Join-Path since it handles adding the path separators for me. But the goal of debugging is to check the data – in your case, make sure that expression is producing an accurate filename. by JMeterX at 2013-04-17 11:44:33 So it appears the$oldfilename is different than any of the Id's that it is saving as though that does not make a lot of sense to me.

Foreach ($GPO in$GPO_Temp_Backup) {$id =$GPOBackup = Backup-GPO -name $GPO.DisplayName -path ($Current_drive + $GPO_Backup_Location + "\" + "{" +$GPO.Id + "}") -domain $domainrename-item -path ($Current_drive + $GPO_Backup_Location + "\" + "{" +$GPO.Id + "}") -newname $GPO.DisplayName}$oldfilename = ($Current_drive +$GPO_Backup_Location + "\" + "{" + $GPO.Id + "}")write$oldfilename

The error is that "Cannot rename because item at C:\Path\to\Location\{unique_id} does not exist

by DonJ at 2013-04-17 11:54:42

That means $Gpo.id is not a string. It's an object of some kind. You can try "$CurrentDrive$GPO_Backup_location$($GPO.id)" or something to see if PowerShell will convert it to a string. by JMeterX at 2013-04-17 12:17:43 Yes, you are correct, I was looking at the site from Microsoft. I tossed in there to writeout$GPOBACKUP and $GPO which showed me some interesting results. It looks like when it goes to rename the item it is pulling the "GpoId" property instead of the ID Though the ID seems to change from the first command to the second. I cannot easily copy and paste the entire blocks I would have to manually type them out so as an example::: The writeout$GPObackup gives me:

C:\PS>Backup-Gpo -Name TestGPO -Path C:\GpoBackups -Comment "Weekly Backup"DisplayName : TestGPOGpoId : 35c12ab3-956c-45d5-973b-46b17d225f47Id : 2b509d4e-40f5-4337-82f7-458584555d0cBackupDirectory : C:\GpoBackupsCreationTime : 2/25/2009 8:48:19 PMDomainName : contoso.comComment : Weekly Backup 

The writeout $GPO gievs me: Id: 35c12ab3-956c-45d5-973b-46b17d225f47DisplayName : TestGPOPath: cn={35c12ab3-956c-45d5-973b-46b17d225f47} The rename says that it was looking for the GpoID, but it saves the files as the ID Any ideas... by JMeterX at 2013-04-17 12:53:14 So it appears it was indeed what I had stated above for whatever reason these ID's change. I corrected the script to be:  Foreach ($GPO in $GPO_Temp_Backup) {$id = $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`

You must be logged in to reply to this topic.