0

我有一台打印服务器,大约有50台打印机安装了各种驱动程序,并不是所有配置都设置相同。我正在将打印机转移到基于新IP架构的新IP地址,并且我需要跟踪每台打印机,旧IP和新IP。然后,我需要捕获每个配置的现有配置,以便我可以添加打印机并保持设置与以前相同。PowerShell脚本抛出错误,但仍然产生结果

所以,这是情况。我用下面的:

PS C:\Users\a> Get-Printer | Where-Object -Property Name -match seattle | Get-PrintConfiguration 

输出是:

Get-PrintConfiguration : An error occurred while performing the specified operation. See the error details for more information. 
At line:1 char:60 
+ Get-Printer | Where-Object -Property Name -match seattle | Get-PrintConfiguratio ... 
+               ~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (MSFT_PrinterConfiguration:ROOT/StandardCi...erConfiguration) [Get-PrintConfiguration], CimException 
+ FullyQualifiedErrorId : HRESULT 0x8000ffff,Get-PrintConfiguration 

Get-PrintConfiguration : An error occurred while performing the specified operation. See the error details for more information. 
At line:1 char:60 
+ Get-Printer | Where-Object -Property Name -match seattle | Get-PrintConfiguratio ... 
+               ~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (MSFT_PrinterConfiguration:ROOT/StandardCi...erConfiguration) [Get-PrintConfiguration], CimException 
+ FullyQualifiedErrorId : HRESULT 0x8000ffff,Get-PrintConfiguration 


PrinterName  ComputerName Collate Color  DuplexingMode  
-----------  ------------ ------- -----  -------------  
Seattle_Coun...     False  True  OneSided    
SeattleWhsLaser     True  True  OneSided    
Seattle Ware...     False  False  OneSided    
Seattle_Seco...     True  False  OneSided    
Seattle_Test...     True  True  OneSided    
SeattleCoun      True  True  OneSided    
Seattle - SH...     True  True  OneSided   

如果我缩短该行:

PS C:\Users\a> Get-Printer | Where-Object -Property Name -match $city 

输出是所有9台打印机如我所料:

Name       ComputerName Type   DriverName    PortName  Shared Published 
----       ------------ ----   ----------    --------  ------ --------- 
Seattle_Test_Printer-Seattl...     Local  HP Universal Printing PS 192.168.100.25 True  True  
Seattle_Second_Floor       Local  HP Universal Printing ... IP_192.168.1... True  True  
Seattle_Counter_Laser       Local  HP Universal Printing ... IP_192.168.1... True  False  
SeattleWhsLaser        Local  HP Universal Printing ... 192.168.100.82 True  True  
SeattleCoun         Local  HP Universal Printing ... IP_192.168.1... True  True  
Seattle Warehouse ZDesigner...     Local  ZDesigner 110XiIII Plu... 192.168.100.... True  True  
Seattle Upstairs OKI PCL6 C...     Local  OKI PCL6 Class Driver  192.168.100.14 True  True  
Seattle - SHARP MX-5141N PCL6     Local  SHARP MX-5141N PCL6  192.168.100.30 True  False  
Seattle (new) HP LaserJet P...     Local  HP LaserJet P3011/P301... 192.168.100.25 True  True  

我应该总共获得9台打印机,但我不明白为什么我得到2台打印机的错误,并为其余的获得好的结果?最终目标是使其自动化并记录所有更改。

+0

当你手动对其中一个运行它时,你能够获得配置设置吗?就像'Get-PrintConfiguration -PrinterName'西雅图(新)HP LaserJet Printer''(您可能需要修复名称,它在您的问题中被截断,所以我刚刚猜到) – TheMadTechnician

+0

实际上没有,但那确实让我发现了我总共有12台打印机,但有9台显示在PS上。在所显示的9个中,缺少的2个似乎错过了他们的驱动程序,因此清除了这些。 (1)我至少有两台打印机没有安装完整的驱动程序(但人们仍然可以打印到它们)(2)我看到12台打印机用于打印此分支mgr(3)失踪3台打印机正常运行。现在我真的很困惑。 @TheMadTechnician – brehma

+0

您可以尝试调用CIM方法直接拉取配置,例如'Get-CimInstance -ClassName MSFT_Printer -Namespace'ROOT/StandardCimv2'|%{Invoke-CimMethod -ClassName MSFT_PrinterConfiguration -Namespace'ROOT/StandardCimv2'-MethodName GetByPrinterName -Arguments @ {'PrinterName'= $ _。Name} |%cmdletOutput}'看看是否有更多的结果。 – TheMadTechnician

回答

0

Get-PrinterGet-PrintConfiguration实际上只是打电话给Get-CimInstance来打电话让我们更容易。我们可以使用CIM对象完成所有这些工作,有时它可以更好地工作。为了让您的打印机,使用此:

Get-CimInstance -ClassName MSFT_Printer -Namespace 'ROOT/StandardCimv2' 

然后你就可以通过管道将成为Where声明(如您所提供)细化的结果,你想要的那些:

Get-CimInstance -ClassName MSFT_Printer -Namespace 'ROOT/StandardCimv2' | where {($_.location -split '\.')[2] -eq $locationNumber -or ($_.sharename -match $city) -or ($_.name -match $city) -or ($_.location -match $city)} 

之后你可以调用MSFT_PrinterConfiguration类的GetByPrinterName方法来获取每个打印机(在ForEach环路)的配置是这样的:

|%{Invoke-CimMethod -ClassName MSFT_PrinterConfiguration -Namespace 'ROOT/StandardCimv2' -MethodName GetByPrinterName -Arguments @{'PrinterName'=$_.Name} |% cmdletOutput} 

所以,如果是我的话我会做这样的事情:

$Printers = Get-CimInstance -ClassName MSFT_Printer -Namespace 'ROOT/StandardCimv2' | where {($_.location -split '\.')[2] -eq $locationNumber -or ($_.sharename -match $city) -or ($_.name -match $city) -or ($_.location -match $city)} 
$Output = @{} 
ForEach($Printer in $Printers){ 
    $Config = Invoke-CimMethod -ClassName MSFT_PrinterConfiguration -Namespace 'ROOT/StandardCimv2' -MethodName GetByPrinterName -Arguments @{'PrinterName'=$Printer.Name} |% cmdletOutput 
    $Printer | Add-Member 'ConfigInfo' $Config 
    $Output.add($Printer.Name,$Printer) 
} 

那么是什么做的是获取所有的印刷机,和过滤器只有你指定的分支。然后它创建一个空的散列表。然后它通过打印机循环,并为每个打印机恢复配置信息。然后将它作为'ConfigInfo'属性添加到打印机对象中。最后,它将修改后的打印机对象作为值向该打印机名称的散列表添加记录。所以最后你可以这样做:

$Output['Seattle_Counter_Laser'] 

而且这将显示打印机的信息,比如驱动程序运行的情况以及端口上列出的IP地址。

或者,如果你想通过IP的端口名称属性,你可以做这样的事情来关注一下吧:

$Output.values|Where{$_.PortName -match '192.168.100.82'} 

然后获取配置信息,你可以这样做:

$Output['Seattle_Counter_Laser'].ConfigInfo 

我会使用Export-CliXml导出整个散列表并将其保存在一个安全的地方,因为如果事情横向并且打印机都停止工作,那么在该文件中您需要一切所需,以便将它们恢复到现在的状态。

+0

非常感谢您花时间分享该剧本以及**如何运作。我会尝试一下,让你知道我已经成功了! @TheMadTechnician – brehma

+0

在PS v4中可能MSFT_PrinterConfiguration被重命名或以其他方式访问吗?我得到一个错误,我发现'-ClassName MSFT_PrinterConfiguration'不是列出的选项。 – brehma

+0

根据操作系统的不同,可能会有所不同。以上是在我的Win10机器上完成的。为了找到答案,对运行的打印机运行'Get-PrintConfiguration'并将其传递给'Get-Member'并查看TypeName的顶部。例如,我的“Microsoft.Management.Infrastructure.CimInstance#ROOT/StandardCimv2/MSFT_PrinterConfiguration”显示。你需要通过#角色的一切。最终的是类,而它之前的是命名空间。所以对于我来说,NameSpace是“ROOT/StandardCimv2”,而类是“MSFT_PrinterConfiguration”。您可能需要为计算机调整这些设置。 – TheMadTechnician