打印机数达数百时的PowerShell和组策略首选项



在管理用户计算机上的网络打印机时,许多副本已损坏。 基本上,管理员分为两个阵营:与登录脚本(bat / vbs)连接以及通过GPP管理。 两种方法都有其优势:脚本处理速度更快,并且GPP比用户重新启动计算机更灵活并且使用频率更高。 但是,当有一百多台打印机,并且它们分散在数十个办公室和城市中时,在两种情况下都会遇到困难。

考虑到每个用户的当前位置,不仅需要将正确的打印机组连接到每个用户,而且也不要忘记任何打印机。 如果有时不仅用户而且打印机本身都被移动了...

总的来说,我和我的同事们首先为自己选择了GPP,这样,除领先的管理员外,任何人都可以通过简单地查看GPMC报告来了解当前的配置。 但是,无论谁说他的标准界面都便于管理100多种设备-让第一个向我投掷石块。 此外,在下一批调试期间,您需要执行许多例程来配置网络扫描并将其添加到打印服务器。

而且,多次完成的所有操作都可以实现自动化!

今天我们要做什么?

  • 保留所有网络打印机的记录;
  • 自动将打印机添加到GPP(PS / XML);
  • 自动将打印机添加到打印服务器以及群集(BAT / VBS)!

因此,让我们开始吧。

1.记帐网络打印机


管理大量任何对象时出现的第一个问题是会计。 如果没有它,很容易对它的安装位置和连接对象感到困惑。
最简单,最通用的解决方案是CSV。 最后,可以从任何清单系统中将ServiceDesk或Excel电子表格上传到CSV,然后轻松地导入到PowerShell中,我们接下来将进行此操作。

我们的卸载看起来像这样:

图片

我将立即解释为什么会有这么多字段:

  • 名称 -打印机的网络名称,它写在DNS中并在打印服务器上
  • ByGroup-该字段确定将打印机连接到相应子网中的每个人,还是仅连接到AD组的成员。 该组还定义了ACL。
  • 子网 -用户必须在其中使用打印机才能使用的子网
  • 位置 -Windows“添加打印机”向导用来搜索打印机的位置线
    如何按位置搜索打印机
    许多人不知道当打印机搜索标准打印机设置向导时会发生什么。 这就是发生的情况。 该向导从AD中的计算机属性中获取“位置”字段,然后从AD中发布的打印机中选择所有位置均在同一行上开始的打印机。 也就是说,如果“鄂木斯克/列宁纳办事处/”,“鄂木斯克/列宁市办事处/ 404”和“鄂木斯克/列宁办事处”列宁/接待处»
  • 驱动程序 -添加到打印服务器时将指定的驱动程序名称。 该驱动程序必须已经安装在服务器上。 通常,所有提供或多或少严肃的打印设备的制造商都具有通用驱动程序,并且每批最多安装一次,如果供应商发生了变化,那么我没有使这部分自动化。
  • 类型只是打印机的描述,以便用户在必要时可以根据其功能轻松选择所需的打印机。
  • 型号 -打印机或MFP的型号 。 在这里,借助辅助表,该字段将填写前两个表。
  • UID-这是今天蛋糕的一部分。 此UID简化了GPP中的对象,以免每次更新组策略时都重新安装打印机。

此数据集足以不在Powershell中对其进行硬编码。 您可以开始玩乐了。

2.自动化GPP


通常,GPP对象中的Printers.xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?> <Printers clsid="{1F577D12-3D1B-471e-A1B7-060317597B9C}"> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BARAB-061" status="PRN-BARAB-061" image="2" uid="{43231EA0-A4A3-4F1A-8A25-95BC4FEFBCC6}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-BARAB-061" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-BARAB-061" sid="S-1-5-21-210359847-7924152125-768726458-48993" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.142.1" max="192.168.142.254" /> </Filters> </SharedPrinter> </Printers> 

可以在这里研究格式的来源: [MS-GPPREF]:打印机 ,在这里: [MS-GPPREF]:通用XML属性

我认为实际上,您可以轻松地将此文件中的字段名称与GPP界面中的复选框进行比较,但我会指出要点:

clsid是组策略首选项类的固定值,可以(并且应该)保持不变。

名称/状态是GPP控制台中元素的显示名称。
uid-但这是您在上表中看到的对象的唯一标识符。

如果每次更新打印机列表时它都更改,则打印机将重新连接到所有用户,这可能导致不同的副作用。
路径 -打印机的UNC路径
FilterGroup,FilterIpRange-通过AD组中的用户成员身份并通过在IP地址范围内查找计算机来定位元素。

2.1。 我们导入数据并创建XML文档的基本结构:


为此,请使用系统类[System.Xml.XmlDocument]:

 $PrintersCSV = Import-Csv -Delimiter ";" ".\Printers.csv" -Encoding Default [System.Xml.XmlDocument]$PrintersGPP = New-Object System.Xml.XmlDocument $PrintersGPP.PrependChild($PrintersGPP.CreateXmlDeclaration("1.0", "utf-8", $null)) | Out-Null $Printers = $PrintersGPP.AppendChild($PrintersGPP.CreateElement("Printers")) $Printers.SetAttribute("clsid","{1F577D12-3D1B-471e-A1B7-060317597B9C}") 

2.2。 添加一些帮助器函数以创建必要的XML元素并设置其属性


创建打印机项目:

 Function NewSharedPrinter { [CmdletBinding()] param ( $SharedPrinter, $clsid = "{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}", $uid, $name, $action ) $image switch ($action){ "C" {$image = 0} "R" {$image = 1} "U" {$image = 2} "D" {$image = 3} } $SharedPrinter.SetAttribute("clsid",$clsid) $SharedPrinter.SetAttribute("name",$name) $SharedPrinter.SetAttribute("status",$name) $SharedPrinter.SetAttribute("image",$image) $SharedPrinter.SetAttribute("uid",$uid) $SharedPrinter.SetAttribute("userContext",1) $SharedPrinter.SetAttribute("bypassErrors",1) $SharedPrinterProperties = $SharedPrinter.AppendChild($PrintersGPP.CreateElement("Properties")) $SharedPrinterProperties.setattribute("action",$action) $SharedPrinterProperties.setattribute("comment","") $SharedPrinterProperties.setattribute("path","\\print-cluster-1.common.domain\$name") $SharedPrinterProperties.setattribute("default",0) $SharedPrinterProperties.setattribute("port","") } 

按组和子网创建过滤(定位)元素:
 Function NewFilterGroup{ [CmdletBinding()] param ( $FilterGroup, $bool = "AND", $not = 0, $name, $sid ) $FilterGroup.SetAttribute("bool",$bool) $FilterGroup.SetAttribute("not",$not) $FilterGroup.SetAttribute("name","COMDOM\"+$name) $FilterGroup.SetAttribute("sid",$sid) $FilterGroup.SetAttribute("userContext",1) } Function NewFilterSubnet{ [CmdletBinding()] param ( $FilterIPRange, $bool = "AND", $not = 0, $start, $end ) $FilterIPRange.SetAttribute("bool",$bool) $FilterIPRange.SetAttribute("not",$not) $FilterIPRange.SetAttribute("min",$start) $FilterIPRange.SetAttribute("max",$end) } 

这三个函数都直接与传输的PS对象一起使用,什么也不返回。 完美主义者的注意事项:在编写脚本时,它变得越来越容易和快捷,我没有尝试编写完美的面向对象的代码,只是需要它可以工作。 是的,代码有待改进,但最重要的是-它可以工作!

2.3。 最后是主循环,添加了打印机控件


为每台打印机创建两个元素:如果用户位于组子网中,则添加一个元素;如果用户不是该组的成员位于另一个子网中,则删除一个打印机。

如果由于某种原因在CSV文件中没有打印机UID,它将被生成并显示在控制台中。

要将子网从CIDR表示法转换为地址范围,我借用了出色的PSipcalc cmdlet。

 ForEach ($PrinterItem in ($PrintersCSV | Sort-Object Name)) { if (!$PrinterItem.uid){ $uid = "{"+([guid]::NewGuid()).Guid.ToUpper()+"}" Write-Host "Printer $($PrinterItem.Name) is new. Policy item ID: $uid" } else { $uid = $PrinterItem.uid } $SharedPrinter = $PrintersGPP.CreateElement("SharedPrinter") NewSharedPrinter -SharedPrinter $SharedPrinter -name $PrinterItem.Name -action "U" -uid $uid $Filters = $SharedPrinter.AppendChild($PrintersGPP.CreateElement("Filters")) if ($PrinterItem.ByGroup -ieq "yes") { try { Get-ADGroup -Identity $PrinterItem.Name | Out-Null } catch { Write-Host "Creating group $($PrinterItem.Name)" New-ADGroup -Name $PrinterItem.Name ` -Path "OU=    ,OU=User Groups,DC=DOM,DC=COM" -GroupScope DomainLocal } $FilterGroup = $PrintersGPP.CreateElement("FilterGroup") NewFilterGroup -FilterGroup $FilterGroup -name $PrinterItem.Name -sid (Get-ADGroup -Identity $PrinterItem.Name).SID.Value $Filters.AppendChild($FilterGroup) | Out-Null } $FilterNetwork = .\PSipcalc.ps1 -NetworkAddress $PrinterItem.Subnet $FilterIPRange = $PrintersGPP.CreateElement("FilterIpRange") NewFilterSubnet -FilterIPRange $FilterIPRange -start $FilterNetwork.HostMin -end $FilterNetwork.HostMax $Filters.AppendChild($FilterIPRange) | Out-Null $Printers.AppendChild($SharedPrinter) | Out-Null $RevertSharedPrinter = $PrintersGPP.CreateElement("SharedPrinter") NewSharedPrinter -SharedPrinter $RevertSharedPrinter -name $PrinterItem.Name -action "D" -uid $uid if ($PrinterItem.ByGroup -ieq "yes") { $bool = "OR" } else { $bool = "AND" } $Filters = $RevertSharedPrinter.AppendChild($PrintersGPP.CreateElement("Filters")) if ($PrinterItem.ByGroup -ieq "yes") { $FilterGroup = $PrintersGPP.CreateElement("FilterGroup") NewFilterGroup -FilterGroup $FilterGroup -name $PrinterItem.Name -sid (Get-ADGroup -Identity $PrinterItem.Name).SID.Value -bool "AND" -not 1 $Filters.AppendChild($FilterGroup) | Out-Null } $FilterIPRange = $PrintersGPP.CreateElement("FilterIpRange") NewFilterSubnet -FilterIPRange $FilterIPRange -start $FilterNetwork.HostMin -end $FilterNetwork.HostMax -bool $bool -not 1 $Filters.AppendChild($FilterIPRange) | Out-Null $Printers.AppendChild($RevertSharedPrinter) | Out-Null } 

2.4。 现在我们将结果打印到文件中


保存几分钟并立即写策略不会出现任何问题:

 $PrintersGPP.Save("\\COMDOM\SysVol\COMMON.DOMAIN\Policies\{f985a9ae-cb71-468b-8a99-e2c7f428aa2f}\User\Preferences\Printers\Printers.xml") 

3.将打印机添加到打印服务器


Windows有一套鲜为人知的用于管理打印机的vbs脚本。 而且,甚至在客户端版本中也是如此。 它位于%SystemRoot%\System32\Printing_Admin_Scripts\en-US目录中,您可以在此处阅读Murzilka。

我们对一组实用程序感兴趣:

prnport.vbs-在打印服务器上创建打印机端口
prnmngr.vbs-自行创建打印机
prncnfg.vbs-配置设置并启用打印机共享
我们还需要SetACL实用程序来设置打印机的访问权限。

3.1。 在打印服务器上创建打印机


为此,创建CreateRemotePrinter.bat脚本。

作为参数,将按以下顺序进行:
%1-打印机的名称;
%2-驱动程序的名称;
%3-位置
%4-打印机说明。

 @echo off SET PRTOOLS="c:\Windows\System32\Printing_Admin_Scripts\en-US" SET SETACL="SetACL.exe" cscript /nologo %PRTOOLS%\prnport.vbs -s print-cl-node-1 -a -r %1 -h %1.common.domain -t -o raw -me cscript /nologo %PRTOOLS%\prnmngr.vbs -s print-cl-node-1 -a -p %1 -m %2 -r %1 cscript /nologo %PRTOOLS%\prncnfg.vbs -s print-cl-node-1 -t -p %1 -h %1 -l %3 -m %4 +shared %SETACL% -on \\print-cl-node-1\%1 -ot prn -actn ace -ace "n:STN-TN\%1;p:man_docs,print" 

在一般情况下,这就足够了。 但是我们的打印服务器位于MSCS群集上,并且上述vbs脚本集不适用于群集。 因此,还需要做其他事情。

3.2。 将打印机从常规打印服务器转移到群集的服务器


当访问群集时,以上设置中的脚本仅在当前活动节点上创建端口和打印机,而不会出现在群集上。 出于这种情况,Microsoft在商店中提供了另一个工具-PrintBRM(打印队列备份/恢复/迁移)。 我没有找到官方文档,因此,我分享了什么: SS64参数的帮助

继续执行CreateRemotePrinter.bat脚本。

PrintBRM实用程序可以将打印机配置以及端口正确复制到群集中,但是,如果您仅进行复制(从-b选项开始)并还原(-r),那么就不会有奇迹了。

因此,现在会有一些魔术。 MS Performance Team Blog上对此进行了详细描述。
首先,我们将打印机备份到temp.printerexport文件。

然后,我们解压缩printerexport子目录,删除LMONS和PRTPROCS目录,并将零个XML文件的内容清零。 我不会详细介绍,但是这些文件包含特定于特定服务器的配置,并且会干扰打印机到另一台服务器(尤其是群集服务器)的恢复。

之后,我们将编辑后的配置重新打包到temp.printerexport文件中,并将其上传到集群:

 SET PRNBRM="C:\Windows\System32\spool\tools\PrintBrm.exe" %PRNBRM% -b -nobin -s print-cl-node-1 -f temp.printerexport %PRNBRM% -r -d printerexport -f temp.printerexport del /f /q temp.printerexport del /s /f /q printerexport\LMONS printerexport\PRTPROCS echo ^<SpoolerAttrib /^> > printerexport\BrmSpoolerAttrib.xml echo ^<PPROCS /^> > printerexport\PProcs.xml echo ^<PRINTERDRIVERS /^> > printerexport\BrmDrivers.xml echo ^<LMONS Arch="Windows x64"/^> > printerexport\BRMLMons.xml %PRNBRM% -b -d printerexport -f temp.printerexport del /s /f /q printerexport %PRNBRM% -r -s print-cluster-1 -f temp.printerexport -p all -o force del /f /q temp.printerexport cscript /nologo %PRTOOLS%\prnmngr.vbs -s print-cl-node-1 -d -p %1 cscript /nologo %PRTOOLS%\prnport.vbs -s print-cl-node-1 -d -r %1 

3.3。 同时将打印机添加到GPP和服务器/群集


现在,您需要将该脚本无缝地适合打印机的CSV列表。

在主循环之前添加请求以获取服务器上现有请求的列表:

 $ActualPrintersList = Get-WmiObject -Class win32_share -computer print-cluster-1 | Where-Object Name -like "*PRN*" | Select-Object -ExpandProperty Name 

在循环的主体中,运行我们的Bat昵称:

  if ("\\print-cluster-1\$($PrinterItem.Name)" -NotIn $ActualPrintersList) { Write-Host "Adding printer $($PrinterItem.Name) to print-cluster-1..." Start-Process -FilePath .\CreateRemotePrinter.bat -ArgumentList "$($PrinterItem.Name) `"$($PrinterItem.Driver)`" `"$($PrinterItem.Location)`" `"$($PrinterItem.Type)`"" -Wait } 

4.放在一起,启动它,得到结果


CSV源数据
Name;ByGroup;Subnet;Location;Driver;Type;Model;uid
PRN-NALTA-028;yes;192.168.192.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M425dn;HP LaserJet Pro M425dn;{35F6CF36-2A24-4A81-B061-8BE71CEC27EA}
PRN-TARUS-002;yes;192.168.128.0/23;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet M3027 MFP;HP LaserJet M3027 MFP;{398F4A94-530C-4E3B-8A30-4288D5E8854D}
PRN-KIRILL-081;;192.168.196.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet 3390;HP LaserJet 3390;{421FC2DE-2E97-49FC-AEB0-3070B0166AD5}
PRN-BARAB-061;yes;192.168.142.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M425dn;HP LaserJet Pro M425dn;{43231EA0-A4A3-4F1A-8A25-95BC4FEFBCC6}
PRN-PYSHM-004;;192.168.143.0/24; /;KX DRIVER for Universal Printing; 4 — Kyocera ECOSYS M2540dn;Kyocera ECOSYS M2540dn;{45509B48-E7BC-4497-9665-86D4E1E96FE1}
PRN-BUY---001;yes;192.168.44.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M402dn;HP LaserJet Pro M402dn;{457DB3FE-E35F-450E-B1D6-912F0D831573}
PRN-BATAY-042;yes;192.168.128.0/23;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet P2015 Series;HP LaserJet P2015 Series;{4740604B-C403-4511-91B0-689A197260F3}
PRN-EMPTY-002;yes;192.168.199.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M425dn;HP LaserJet Pro M425dn;{475578CB-689F-463B-9710-AE207973146C}
PRN-LIPKI-003;;192.168.44.0/24;/;KX DRIVER for Universal Printing; 4 — Kyocera ECOSYS M2540dn;Kyocera ECOSYS M2540dn;{4794D22E-6586-4A81-A85D-A21FB8B209CF}
PRN-KOTOV-013;yes;192.168.128.0/23;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M425dn;HP LaserJet Pro M425dn;{4DFFBA1F-48D2-4824-B2EB-0AAD12B6A9D6}
PRN-ELAB-064;;192.168.140.0/24;/;HP Universal Printing PCL 6 (v5.6.0); 4 — HP LaserJet Pro M425dn;HP LaserJet Pro M425dn;{52069D45-EF86-442D-A157-368D7510A1CF}
GeneratePrintersXml.ps1
 $PrintersCSV = Import-Csv -Delimiter ";" ".\Printers.csv" -Encoding Default [System.Xml.XmlDocument]$PrintersGPP = New-Object System.Xml.XmlDocument $PrintersGPP.PrependChild($PrintersGPP.CreateXmlDeclaration("1.0", "utf-8", $null)) | Out-Null $Printers = $PrintersGPP.AppendChild($PrintersGPP.CreateElement("Printers")) $Printers.SetAttribute("clsid","{1F577D12-3D1B-471e-A1B7-060317597B9C}") $DelPrinters = $Printers.AppendChild($PrintersGPP.CreateElement("SharedPrinter")) NewSharedPrinter -SharedPrinter $DelPrinters -name "Delete All" -action "D" -uid "{21097DBD-285D-48C3-B042-7746D7E6DA1B}" $Filters = $DelPrinters.AppendChild($PrintersGPP.CreateElement("Filters")) $FilterRunOnce = $Filters.AppendChild($PrintersGPP.CreateElement("FilterRunOnce")) $FilterRunOnce.SetAttribute("id","{F2537B78-C7D7-43DF-98D6-B32E90644825}") $FilterRunOnce.SetAttribute("hidden",1) $FilterRunOnce.SetAttribute("not",0) $FilterRunOnce.SetAttribute("bool","AND") Function NewSharedPrinter { [CmdletBinding()] param ( $SharedPrinter, $clsid = "{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}", $uid, $name, $action ) $image switch ($action){ "C" {$image = 0} "R" {$image = 1} "U" {$image = 2} "D" {$image = 3} } $SharedPrinter.SetAttribute("clsid",$clsid) $SharedPrinter.SetAttribute("name",$name) $SharedPrinter.SetAttribute("status",$name) $SharedPrinter.SetAttribute("image",$image) $SharedPrinter.SetAttribute("uid",$uid) $SharedPrinter.SetAttribute("userContext",1) $SharedPrinter.SetAttribute("bypassErrors",1) $SharedPrinterProperties = $SharedPrinter.AppendChild($PrintersGPP.CreateElement("Properties")) $SharedPrinterProperties.setattribute("action",$action) $SharedPrinterProperties.setattribute("comment","") $SharedPrinterProperties.setattribute("path","\\print-cluster-1.common.domain\$name") $SharedPrinterProperties.setattribute("default",0) $SharedPrinterProperties.setattribute("port","") } Function NewFilterGroup{ [CmdletBinding()] param ( $FilterGroup, $bool = "AND", $not = 0, $name, $sid ) $FilterGroup.SetAttribute("bool",$bool) $FilterGroup.SetAttribute("not",$not) $FilterGroup.SetAttribute("name","COMDOM\"+$name) $FilterGroup.SetAttribute("sid",$sid) $FilterGroup.SetAttribute("userContext",1) } Function NewFilterSubnet{ [CmdletBinding()] param ( $FilterIPRange, $bool = "AND", $not = 0, $start, $end ) $FilterIPRange.SetAttribute("bool",$bool) $FilterIPRange.SetAttribute("not",$not) $FilterIPRange.SetAttribute("min",$start) $FilterIPRange.SetAttribute("max",$end) } $ActualPrintersList = Get-WmiObject -Class win32_share -computer print-cluster-1 | Where-Object Name -like "*PRN*" | Select-Object -ExpandProperty Name ForEach ($PrinterItem in ($PrintersCSV | Sort-Object Name)) { if (!$PrinterItem.uid){ $uid = "{"+([guid]::NewGuid()).Guid.ToUpper()+"}" Write-Host "Printer $($PrinterItem.Name) is new. Policy item ID: $uid" } else { $uid = $PrinterItem.uid } $SharedPrinter = $PrintersGPP.CreateElement("SharedPrinter") NewSharedPrinter -SharedPrinter $SharedPrinter -name $PrinterItem.Name -action "U" -uid $uid $Filters = $SharedPrinter.AppendChild($PrintersGPP.CreateElement("Filters")) if ($PrinterItem.ByGroup -ieq "yes") { try { Get-ADGroup -Identity $PrinterItem.Name | Out-Null } catch { Write-Host "Creating group $($PrinterItem.Name)" New-ADGroup -Name $PrinterItem.Name ` -Path "OU=    ,OU=User Groups,DC=DOM,DC=COM" -GroupScope DomainLocal } $FilterGroup = $PrintersGPP.CreateElement("FilterGroup") NewFilterGroup -FilterGroup $FilterGroup -name $PrinterItem.Name -sid (Get-ADGroup -Identity $PrinterItem.Name).SID.Value $Filters.AppendChild($FilterGroup) | Out-Null } $FilterNetwork = .\PSipcalc.ps1 -NetworkAddress $PrinterItem.Subnet $FilterIPRange = $PrintersGPP.CreateElement("FilterIpRange") NewFilterSubnet -FilterIPRange $FilterIPRange -start $FilterNetwork.HostMin -end $FilterNetwork.HostMax $Filters.AppendChild($FilterIPRange) | Out-Null $Printers.AppendChild($SharedPrinter) | Out-Null $RevertSharedPrinter = $PrintersGPP.CreateElement("SharedPrinter") NewSharedPrinter -SharedPrinter $RevertSharedPrinter -name $PrinterItem.Name -action "D" -uid $uid if ($PrinterItem.ByGroup -ieq "yes") { $bool = "OR" } else { $bool = "AND" } $Filters = $RevertSharedPrinter.AppendChild($PrintersGPP.CreateElement("Filters")) if ($PrinterItem.ByGroup -ieq "yes") { $FilterGroup = $PrintersGPP.CreateElement("FilterGroup") NewFilterGroup -FilterGroup $FilterGroup -name $PrinterItem.Name -sid (Get-ADGroup -Identity $PrinterItem.Name).SID.Value -bool "AND" -not 1 $Filters.AppendChild($FilterGroup) | Out-Null } $FilterIPRange = $PrintersGPP.CreateElement("FilterIpRange") NewFilterSubnet -FilterIPRange $FilterIPRange -start $FilterNetwork.HostMin -end $FilterNetwork.HostMax -bool $bool -not 1 $Filters.AppendChild($FilterIPRange) | Out-Null $Printers.AppendChild($RevertSharedPrinter) | Out-Null if ("\\print-cluster-1\$($PrinterItem.Name)" -NotIn $ActualPrintersList) { Write-Host "Adding printer $($PrinterItem.Name) to print-cluster-1..." Start-Process -FilePath .\CreateRemotePrinter.bat -ArgumentList "$($PrinterItem.Name) `"$($PrinterItem.Driver)`" `"$($PrinterItem.Location)`" `"$($PrinterItem.Type)`"" -Wait } } $PrintersGPP.Save("\\COMDOM\SysVol\COMMON.DOMAIN\Policies\{f985a9ae-cb71-468b-8a99-e2c7f428aa2f}\User\Preferences\Printers\Printers.xml") $PrintersGPP.Save(".\Printers.xml") 
CreateRemotePrinter.bat
 @echo off SET PRTOOLS="c:\Windows\System32\Printing_Admin_Scripts\en-US" SET SETACL="SetACL.exe" SET PRNBRM="C:\Windows\System32\spool\tools\PrintBrm.exe" cscript /nologo %PRTOOLS%\prnport.vbs -s print-cl-node-1 -a -r %1 -h %1.common.domain -t -o raw -me cscript /nologo %PRTOOLS%\prnmngr.vbs -s print-cl-node-1 -a -p %1 -m %2 -r %1 cscript /nologo %PRTOOLS%\prncnfg.vbs -s print-cl-node-1 -t -p %1 -h %1 -l %3 -m %4 +shared %SETACL% -on \\print-cl-node-1\%1 -ot prn -actn ace -ace "n:STN-TN\%1;p:man_docs,print" %PRNBRM% -b -nobin -s print-cl-node-1 -f temp.printerexport %PRNBRM% -r -d printerexport -f temp.printerexport del /f /q temp.printerexport del /s /f /q printerexport\LMONS printerexport\PRTPROCS echo ^<SpoolerAttrib /^> > printerexport\BrmSpoolerAttrib.xml echo ^<PPROCS /^> > printerexport\PProcs.xml echo ^<PRINTERDRIVERS /^> > printerexport\BrmDrivers.xml echo ^<LMONS Arch="Windows x64"/^> > printerexport\BRMLMons.xml %PRNBRM% -b -d printerexport -f temp.printerexport del /s /f /q printerexport %PRNBRM% -r -s print-cluster-1 -f temp.printerexport -p all -o force del /f /q temp.printerexport cscript /nologo %PRTOOLS%\prnmngr.vbs -s print-cl-node-1 -d -p %1 cscript /nologo %PRTOOLS%\prnport.vbs -s print-cl-node-1 -d -r %1 
生成的文件Printers.xml
 <?xml version="1.0" encoding="utf-8"?> <Printers clsid="{1F577D12-3D1B-471e-A1B7-060317597B9C}"> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="Delete All" status="Delete All" image="3" uid="{21097DBD-285D-48C3-B042-7746D7E6DA1B}" bypassErrors="1" disabled="1"> <Properties action="D" path="" default="0" deleteAll="1" port="" /> <Filters> <FilterRunOnce id="{F2537B78-C7D7-43DF-98D6-B32E90644825}" hidden="1" not="0" bool="AND" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BARAB-061" status="PRN-BARAB-061" image="2" uid="{43231EA0-A4A3-4F1A-8A25-95BC4FEFBCC6}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-BARAB-061" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-BARAB-061" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.142.1" max="192.168.142.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BARAB-061" status="PRN-BARAB-061" image="3" uid="{43231EA0-A4A3-4F1A-8A25-95BC4FEFBCC6}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-BARAB-061" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-BARAB-061" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.142.1" max="192.168.142.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BATAY-042" status="PRN-BATAY-042" image="2" uid="{4740604B-C403-4511-91B0-689A197260F3}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-BATAY-042" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-BATAY-042" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BATAY-042" status="PRN-BATAY-042" image="3" uid="{4740604B-C403-4511-91B0-689A197260F3}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-BATAY-042" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-BATAY-042" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BUY---001" status="PRN-BUY---001" image="2" uid="{457DB3FE-E35F-450E-B1D6-912F0D831573}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-BUY---001" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-BUY---001" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.44.1" max="192.168.44.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-BUY---001" status="PRN-BUY---001" image="3" uid="{457DB3FE-E35F-450E-B1D6-912F0D831573}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-BUY---001" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-BUY---001" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.44.1" max="192.168.44.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-ELAB-064" status="PRN-ELAB-064" image="2" uid="{52069D45-EF86-442D-A157-368D7510A1CF}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-ELAB-064" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="0" min="192.168.140.1" max="192.168.140.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-ELAB-064" status="PRN-ELAB-064" image="3" uid="{52069D45-EF86-442D-A157-368D7510A1CF}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-ELAB-064" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="1" min="192.168.140.1" max="192.168.140.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-EMPTY-002" status="PRN-EMPTY-002" image="2" uid="{475578CB-689F-463B-9710-AE207973146C}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-EMPTY-002" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-EMPTY-002" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.199.1" max="192.168.199.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-EMPTY-002" status="PRN-EMPTY-002" image="3" uid="{475578CB-689F-463B-9710-AE207973146C}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-EMPTY-002" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-EMPTY-002" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.199.1" max="192.168.199.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-KIRILL-081" status="PRN-KIRILL-081" image="2" uid="{421FC2DE-2E97-49FC-AEB0-3070B0166AD5}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-KIRILL-081" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="0" min="192.168.196.1" max="192.168.196.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-KIRILL-081" status="PRN-KIRILL-081" image="3" uid="{421FC2DE-2E97-49FC-AEB0-3070B0166AD5}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-KIRILL-081" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="1" min="192.168.196.1" max="192.168.196.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-KOTOV-013" status="PRN-KOTOV-013" image="2" uid="{4DFFBA1F-48D2-4824-B2EB-0AAD12B6A9D6}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-KOTOV-013" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-KOTOV-013" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-KOTOV-013" status="PRN-KOTOV-013" image="3" uid="{4DFFBA1F-48D2-4824-B2EB-0AAD12B6A9D6}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-KOTOV-013" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-KOTOV-013" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-LIPKI-003" status="PRN-LIPKI-003" image="2" uid="{4794D22E-6586-4A81-A85D-A21FB8B209CF}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-LIPKI-003" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="0" min="192.168.44.1" max="192.168.44.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-LIPKI-003" status="PRN-LIPKI-003" image="3" uid="{4794D22E-6586-4A81-A85D-A21FB8B209CF}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-LIPKI-003" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="1" min="192.168.44.1" max="192.168.44.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-NALTA-028" status="PRN-NALTA-028" image="2" uid="{35F6CF36-2A24-4A81-B061-8BE71CEC27EA}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-NALTA-028" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-NALTA-028" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.192.1" max="192.168.192.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-NALTA-028" status="PRN-NALTA-028" image="3" uid="{35F6CF36-2A24-4A81-B061-8BE71CEC27EA}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-NALTA-028" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-NALTA-028" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.192.1" max="192.168.192.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-PYSHM-004" status="PRN-PYSHM-004" image="2" uid="{45509B48-E7BC-4497-9665-86D4E1E96FE1}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-PYSHM-004" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="0" min="192.168.143.1" max="192.168.143.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-PYSHM-004" status="PRN-PYSHM-004" image="3" uid="{45509B48-E7BC-4497-9665-86D4E1E96FE1}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-PYSHM-004" default="0" port="" /> <Filters> <FilterIpRange bool="AND" not="1" min="192.168.143.1" max="192.168.143.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-TARUS-002" status="PRN-TARUS-002" image="2" uid="{398F4A94-530C-4E3B-8A30-4288D5E8854D}" userContext="1" bypassErrors="1"> <Properties action="U" comment="" path="\\print-cluster-1.common.domain\PRN-TARUS-002" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="0" name="COMDOM\PRN-TARUS-002" sid="" userContext="1" /> <FilterIpRange bool="AND" not="0" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> <SharedPrinter clsid="{9A5E9697-9095-436d-A0EE-4D128FDFBCE5}" name="PRN-TARUS-002" status="PRN-TARUS-002" image="3" uid="{398F4A94-530C-4E3B-8A30-4288D5E8854D}" userContext="1" bypassErrors="1"> <Properties action="D" comment="" path="\\print-cluster-1.common.domain\PRN-TARUS-002" default="0" port="" /> <Filters> <FilterGroup bool="AND" not="1" name="COMDOM\PRN-TARUS-002" sid="" userContext="1" /> <FilterIpRange bool="OR" not="1" min="192.168.128.1" max="192.168.129.254" /> </Filters> </SharedPrinter> </Printers> 
打印服务器上的结果
图片

Source: https://habr.com/ru/post/zh-CN421211/


All Articles