创建打印机Powershell

Tim*_*der 5 powershell wmi

我正在努力成功创建一个打印机的脚本时遇到很大困难.到目前为止,我设法成功创建了打印机端口,但创建打印机时总是出错.

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

PrinterName,PrinterPort,IPAddress,Location,Comment,PrintDriver,
Testprint1,Testport1,10.10.10.10,IT_Test,Test_1,HP LaserJet 4200 PCL 5e,
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息是这样的:

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

我使用的代码是这样的:

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 
} 
Run Code Online (Sandbox Code Playgroud)

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

RRU*_*RUZ 1

蒂姆,Generic failure当错误的参数传递给 WMI 方法时,会引发错误,因此有两个建议,首先尝试使用真实的端口名称,Testport1然后检查 DriverName 是否必须是现有驱动程序的确切名称。