Edit/Add 通过 Powershell 在 DC 上的 GPO

Edit/Add GPO on DC via Powershell

我的任务是通过 GPO 将工作站上安装的所有打印机迁移到另一台服务器。 至于现在所有的打印机都安装在一个本地分散的分发点,我们想转移到一个集中的分发 Point/Print 服务器上。 在我的 DC 上,通过组策略管理编辑器,我在

中有很多打印机

Computer Configuration\Preferences\Control Panel Settings\Printers

所有打印机都从 \DP00x\Printer 映射并指定一个本地名称。

我要更改的是 GPO 中的 \DP00x 到 \CentralDP01\Printer

我已经通过 powershell 创建了所有打印机端口,安装了所有打印机,并且 publish/list 在所有这些目录中。 鉴于它们超过100个,我希望将编辑GPO的过程自动化,这样我就不需要打开每个策略和每个打印机来修改目的地。

我尝试了 cmdlet Get-GPRegistryValue 因为我知道(至少)打印机安装在 HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers

但我每次都收到这个错误:

Get-GPRegistryValue : The following Group Policy registry setting was not found: "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers".
Parameter name: keyPath
At line:1 char:1
+ Get-GPRegistryValue -Guid 6b464ed9-66c8-47fa-8327-1fe9b074a0d7 -Key H...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Microsoft.Group...tryValueCommand:GetGPRegistryValueCommand) [Get-GPRegistryValue], ArgumentException
    + FullyQualifiedErrorId : UnableToRetrievePolicyRegistryItem,Microsoft.GroupPolicy.Commands.GetGPRegistryValueCommand

我也试过了 Get-GPPrefRegistryValue

Get-GPPrefRegistryValue -Context Computer -Guid 6b464ed9-66c8-47fa-8327-1fe9b074a0d7 -Key HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers

但错误看起来是一样的:

Get-GPPrefRegistryValue : The Preference registry setting "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers" was not found in the 
"x-x-x-x-x-x" GPO in the x-x-x-x-x-x-x.com domain.
Parameter name: keyPath
At line:1 char:1
+ Get-GPPrefRegistryValue -Context Computer -Guid 6b464ed9-66c8-47fa-83 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Microsoft.Group...tryValueCommand:GetGPPrefRegistryValueCommand) [Get-GPPrefRegistryValue], ArgumentException
    + FullyQualifiedErrorId : UnableToRetrievePreferenceRegistryItem,Microsoft.GroupPolicy.Commands.GetGPPrefRegistryValueCommand

我找到了解决方法。备份 GPO,使用新值手动编辑 XML 并导回 GPO。 我不喜欢手动编辑的想法,因为它会导致错误,并且对于超过 100 个 GPO,我可能会遇到很多错误。 谁能帮我? 也许我使用了错误的命令,但到目前为止文档说明使用 GPO 模块。

不幸的是,GroupPolicy 命令仅限于注册表项设置,打印机首选项不在其中。不过,您可以自己安全地编辑实时 GPO xml 文件(或使用 Backup-GPO/Restore-GPO)。

如果您只是替换服务器名称,这应该没问题。在测试 GPO 上试用,根据需要更新路径:

$guid = (Get-GPO -Name 'Test GPO')

# Check the GPO version before changes:
Get-GPO -guid $guid

$domain = 'domain.com'
$path = "\$domain\SYSVOL$domain\Policies\{$guid}\User\Preferences\Printers\Printers.xml"

# Update the path in the GPO xml:
(Get-Content $path -Raw) -replace 'DP00x','CentralDP01' | Set-Content $path

# Validate the GPO version/change date have updated - might take a while if xml is on a different DC:
Get-GPO -guid $guid