基于GPO DisplayName而不是ID的备份GPO [英] Backup GPOs basing on GPO DisplayName rather than Id

查看:143
本文介绍了基于GPO DisplayName而不是ID的备份GPO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在备份域控制器的所有GPO,并且我注意到Backup-GPO cmdlet备份GPOS的方式非常不友好.默认情况下,它为每个以"ID"命名的GPO都创建一个文件夹,该文件夹甚至与其"GPOID/GUID"都不匹配.

So I am backing up all the GPOs of a Domain Controller and I noticed that the way Backup-GPO cmdlet backs up the GPOS is so unfriendly. By default it creates a folder for every single GPO named after the "ID" which doesn't even match its "GPOID/GUID".

这里是一个示例,我将仅备份特定的GPO:

Here is an example, I will just backup a specific GPO:

backup-gpo -guid ff8de365-0842-46ab-9ac7-64ebd8dd4614 -path C:\


DisplayName     : N12 Workstation Policy
GpoId           : ff8de365-0842-46ab-9ac7-64ebd8dd4614
Id              : dd33c220-bac8-4ebd-a9d9-7729fcea9c38
BackupDirectory : C:\
CreationTime    : 20/10/2015 17:41:43
DomainName      : martyn.local

这是在发出上一条命令之后创建的备份文件夹名称:

This is the backup folder name that is created after issuing the previous command:

{DD33C220-BAC8-4EBD-A9D9-7729FCEA9C38}

如果我尝试备份所有GPO,我将为每个单个GPO获得一个文件夹.有什么方法可以根据GPO DisplayName而不是不友好的字符串来命名这些文件夹?

If I try to Backup all the GPOs I get a folder for every single GPO. Is there any way of naming these folders basing on the GPO DisplayName rather than that unfriendly string?

这就是我想要得到的:

N12 Workstation Policy

之所以要这样做,是因为如果将来我想重新导入一个GPO,而我不记得GPO的名称,我应该如何知道哪个才是正确的GPO.如果我使用的是可怕的名称,该文件夹将导入吗?

The reason why I want to do it like that is because if I want to re-import a single GPO in the future and I don't remember the name of the GPO how am I supposed to know which is the right GPO backup folder to import if I am using that awful name?

谢谢

推荐答案

将每个GPO备份到备份文件夹的单独子文件夹中:

Backup each GPO to separate subfolders of your backup folder:

$invalidChars = ':\\/' + [RegEx]::Escape(-join [IO.Path]::InvalidPathChars)

$backupDir = 'C:\backup'
Get-GPO -All | ForEach-Object {
  $name = $_.DisplayName -replace "[$invalidChars]", '_'
  $gpoDir = Join-Path $backupDir -ChildPath $name
  New-Item $gpoDir -Type Directory | Out-Null
  Backup-GPO -Guid $_.Id -Path $gpoDir
}

替换操作是从名称中删除路径中无效的字符(如果包含GPO的名称(例如>),则子文件夹的创建将失败).正如@briantist在他的评论中建议的那样,最好是从相应的IO.Path属性中派生无效字符列表,而不是手工维护该列表.但是,您可能需要手动添加其他有问题的字符,特别是:.冒号用于访问备用数据流,因此从技术上讲,它是路径中的有效字符,但PowerShell不支持.

The replacement is to remove characters that are invalid in a path from the name (the creation of the subfolder would fail if the name of the GPO contained for instance a >). As @briantist suggested in his comment it's better to derive the list of invalid characters from the respective IO.Path property than maintain the list by hand. You may need to manually add other problematic characters, though, specifically :. The colon is used for accessing alternate data streams, so it's technically a valid character in a path, but PowerShell doesn't support it.

这篇关于基于GPO DisplayName而不是ID的备份GPO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆