创建打印机Powershell

时间:2011-03-31 15:00:50

标签: powershell wmi

我正在努力获取成功创建打印机的脚本。到目前为止,我已成功创建了打印机端口,但创建打印机时总是出错。

从中获取信息的CSV文件如下所示:

PrinterName,PrinterPort,IPAddress,Location,Comment,PrintDriver,
Testprint1,Testport1,10.10.10.10,IT_Test,Test_1,HP LaserJet 4200 PCL 5e,

我得到的错误信息是:

Exception calling "Put" with "0" argument(s): "Generic failure "
At C:\myversion.ps1:53 char:19
+ $objNewPrinter.Put <<<< () 
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我使用的代码是:

trap { $error[0].Exception | get-member } $objNewPrinter.Put()

Write-Host ("`tCreating " + $strPrinterName.Count + " Printer's`t") 
$objPrint = [WMICLASS]"\\timvista\ROOT\cimv2:Win32_Printer"   
$objPrint.psbase.scope.options.EnablePrivileges = $true 

## Loop through and create each printer 
For($i=0; $i -lt $PrinterName.count; $i++) 
{ 
$objNewPrinter = $objPrint.createInstance() 
$objNewPrinter.DeviceID = $PrinterName[$i] ## This is the printer name 
$objNewPrinter.DriverName = $PrintDriver[$i] 
$objNewPrinter.PortName = $PrinterPort[$i] 
$objNewPrinter.Location = $Location[$i] 
$objNewPrinter.Comment = $Comment[$i] 
$objNewPrinter.ShareName = $PrinterName[$i] 
$objNewPrinter.Put() 
$objPrint
## Add Security group 
## Not completed yet 
} 

有没有人对通用故障是什么以及如何对其进行故障排除有任何想法?

1 个答案:

答案 0 :(得分:1)

Tim,当错误的参数传递给WMI方法时会引发Generic failure错误,所以有两个建议,首先尝试使用真实的端口名而不是Testport1并检查DriverName必须是准确的现有驱动程序的名称。