Powershell v2 - 安装打印机

时间:2013-11-12 12:19:20

标签: powershell printers

我正在尝试使用Powershell脚本在Windows 7 x64上自动执行打印机安装。到目前为止,我有一个成功创建TCP / IP端口的脚本但是在执行代码的打印机安装部分时给出了一个错误 - 参数无效。有关如何解决问题并通过Powershell成功安装打印机的任何想法?代码如下:

$hostAddress = "172.16.2.24" 
$portNumber = "9100"  
$computer = $env:COMPUTERNAME 

$wmi= [wmiclass]"\\$computer\root\cimv2:win32_tcpipPrinterPort" 
#$wmi.psbase.scope.options.enablePrivileges = $true 
$newPort = $wmi.createInstance() 

$newPort.hostAddress = $hostAddress 
$newPort.name = "IP_" + $hostAddress 
$newPort.portNumber = $portNumber 
$newPort.SNMPEnabled = $false 
$newPort.Protocol = 1 
$newPort.put()

CMD.EXE /C "printui.exe /if /b "Test Printer" /f C:\inetpub\wwwroot\ftp\Prdrivers\HP Universal Print Driver\pcl6-x64-5.7.0.16448\hpbuio100l.inf /r "IP_172.16.2.24" /m "HP Laser Jet P3015""

问题更新:这是有效的CMD代码,那么如何将其合并到上面的Powershell代码中呢?

printui.exe /if /b "HP Universal Printing PCL 6" /f "C:\inetpub\wwwroot\ftp\Prdrivers\HP Universal Print Driver\pcl6-x64-5.7.0.16448\hpbuio100l.inf" /u /r "IP_172.16.2.24" /m "HP Universal Printing PCL 6"

4 个答案:

答案 0 :(得分:2)

要在双引号字符串中嵌入双引号,您需要转义它们。由于您没有使用变量,因此使用单引号字符串会更容易,例如:

CMD.EXE /C 'printui.exe /if /b "Test Printer" /f C:\inetpub\wwwroot\ftp\Prdrivers\HP Universal Print Driver\pcl6-x64-5.7.0.16448\hpbuio100l.inf /r "IP_172.16.2.24" /m "HP Laser Jet P3015"'

如果你需要在这个字符串中使用PowerShell变量,那么你需要切换回双引号并转义必要的DQ字符,例如:

CMD.EXE /C "printui.exe /if /b `"$PrinterName`" /f C:\inetpub\wwwroot\ftp\Prdrivers\HP Universal Print Driver\pcl6-x64-5.7.0.16448\hpbuio100l.inf /r `"IP_172.16.2.24`" /m `"HP Laser Jet P3015`""

答案 1 :(得分:2)

抱歉,我不知道你为什么要打电话给CMD / C @PARAMS。我只是直接调用printui.exe并且它正在工作,我只引用了Args

# Printer Info, I keep this in an SQL DB, and return these values with a query:
$printerID = "<PrinterNameOrID>"
$printerIP = "<PrinterIP>"
$printerPort = "IP_$printerIP"
$printerModel = "<PrinterModelFromINF>"
$driverINFPath = "<UNCPathToDriverINF>"

# Build a new Local TCP Printer Port, naming it with values unique to the Printer ID:
$newPort = ([wmiclass]"Win32_TcpIpPrinterPort").CreateInstance()
$newPort.HostAddress = $printerIP
$newPort.Name = $printerPort
$newPort.PortNumber = "9100"
$newPort.Protocol = 1
$newPort.Put()

# Add the printer
printui.exe /if /b "$printerID" /f "$driverINFPath" /u /r "$printerPort" /m "$printerModel"

答案 2 :(得分:1)

我知道这已经得到了解答,但你可以借用我在这个Excel工作簿中的代码(文章中有一个链接)。我意识到它使用的是VBS,但这些都是用Windows中的脚本构建的,切割/粘贴到Excel中已经多次保存了我,并且我已经安装了数千台打印机

best-tool-for-printer-creation-excel-vs-print-management-console

答案 3 :(得分:0)

试试这个:

runas /user:Contoso.com\user1 "printui.exe /if /b \"Test Printer\" /f \"C:\inetpub\wwwroot\ftp\Prdrivers\HP Universal Print Driver\pcl6-x64-5.7.0.16448\hpbuio100l.inf\" /r \"IP_172.16.2.24\" /m \"HP Laser Jet P3015\""