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