简体   繁体   English

通过displayname而不是ID备份GPO - powershell

[英]Backup GPO by displayname and not ID - powershell

I want to backup GPOs using their names instead of their ID's. 我想使用他们的名称而不是他们的ID来备份GPO。 Currently I have: 目前我有:

$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 截至目前,它仍然支持GPO作为他们的ID

So I decided to change the code a bit so I could debug what was going on. 所以我决定稍微更改代码,以便我可以调试正在进行的操作。 I added writeout $GPOBackup and writeout $GPO . 我添加了writeout $GPOBackupwriteout $GPO Interestingly this is similar to what I saw. 有趣的是,这与我所看到的相似。 (Example taken from Microsoft page) (摘自Microsoft页面的示例)

writeout $GPOBackup provided me with: writeout $GPOBackup为我提供:

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: writeout $GPO为我提供:

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. 正如您在第一个输出中看到的那样,有一个GpoId和一个Id。 The output of the second command suddenly changes the Id to what was originally the GpoID. 第二个命令的输出突然将Id更改为最初的GpoID。 When the backup-gpo command saves the GPO it saves it using the Id that you receive from the first block of code. backup-gpo命令保存GPO时,它使用从第一个代码块收到的Id来保存它。 The Id then changes to what is the GpoId . Id随后更改为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 以下代码允许我将backup GPOsDisplayName

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

Here's a little script I wrote to backup all GPOs in the domain. 这是我编写的用于备份域中所有GPO的小脚本。 This takes advantage of the manifest.xml file created by Backup-GPO to get around the Id issue: 这利用了Backup-GPO创建的manifest.xml文件来解决Id问题:

[CmdletBinding()]
param(
    [parameter()]
    [ValidateScript({Test-Path $_ -PathType Container})]
    [string]$Path = 'C:\GroupPolicy'
)

Get-GPO -All | Backup-GPO -Path $Path -Comment ('Backup taken: {0}' -f (Get-Date -Format G))
[xml]$manifest = Get-Content (Join-Path $Path 'manifest.xml')
foreach ($gpBackup in $manifest.Backups.BackupInst) {
    Rename-Item -Path (Join-Path $Path $gpBackup.Id.InnerText) -NewName ($gpBackup.GPODisplayName.InnerText -replace '[:\\/]','')
}

I think you might just use the $gpobackup.id to get the guid, convert it to folder path and then simply use rename. 我想你可能只是使用$ gpobackup.id获取guid,将其转换为文件夹路径,然后只使用重命名。 Example: 例:

$GPOBackup = Backup-GPO -name $GPO.DisplayName -path ($Current_Drive + $GPO_Backup_Location) -Domain $domain;
$guidpath = $($Current_Drive + $GPO_Backup_Location + '{' + $x.id.tostring() + '}' )
$newpath = $($Current_Drive + $GPO_Backup_Location + $GPOBackup.DisplayName)
rename-item -Path $guidpath -newname $newpath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM