繁体   English   中英

Powershell v2-安装打印机

[英]Powershell v2 - Installing Printer

我正在尝试使用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"

要将双引号嵌入双引号字符串中,您需要对其进行转义。 由于您没有使用变量,因此使用单引号的字符串会更容易,例如:

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`""

抱歉,但是我不确定为什么要调用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"

我知道已经回答了这个问题,但是您可以借用此Excel工作簿中的代码(本文中有链接)。 我意识到它使用VBS,但是这些都是Windows中内置的脚本,剪切/粘贴到Excel中已经为我节省了很多时间,并且我已经以这种方式安装了数千台打印机

打印机创建的最佳工具,优于打印管理控制台

尝试这个:

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\""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM