简体   繁体   English

基于GPO DisplayName而不是Id的备份GPO

[英]Backup GPOs basing on GPO DisplayName rather than Id

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. 所以我正在备份域控制器的所有GPO,我注意到Backup-GPO cmdlet备份GPOS的方式非常不友好。 By default it creates a folder for every single GPO named after the "ID" which doesn't even match its "GPOID/GUID". 默认情况下,它为每个以“ID”命名的GPO创建一个文件夹,该ID甚至不匹配其“GPOID / GUID”。

Here is an example, I will just backup a specific GPO: 这是一个例子,我将备份一个特定的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}

If I try to Backup all the GPOs I get a folder for every single GPO. 如果我尝试备份所有GPO,我会为每个GPO获取一个文件夹。 Is there any way of naming these folders basing on the GPO DisplayName rather than that unfriendly string? 有没有办法根据GPO DisplayName命名这些文件夹而不是那个不友好的字符串?

This is what I would like to get: 这就是我想得到的:

N12 Workstation Policy

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并且我不记得GPO的名称我怎么知道哪个是正确的GPO备份文件夹到如果我使用那个可怕的名字导入?

Thanks 谢谢

Backup each GPO to separate subfolders of your backup folder: 将每个GPO备份到备份文件夹的单独子文件夹:

$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
}

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 > ). 替换是从名称中删除路径中无效的字符(如果GPO的名称包含例如> ,则子文件夹的创建将失败)。 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. 正如@briantist在他的评论中建议的那样,最好从相应的IO.Path属性中导出无效字符列表,而不是手动维护列表。 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. 冒号用于访问备用数据流 ,因此从技术上讲它是路径中的有效字符,但PowerShell不支持它。

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

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