如何在Windows 7上使用VBScript添加本地打印机?

时间:2012-12-05 15:14:58

标签: windows-7 vbscript batch-file zebra-printers printers

我有一台Windows 7 64位PC,我正在尝试添加一台本地打印机,它会自动安装驱动程序并在完成后共享打印机。 该端口是Loopback IP地址(127.0.0.1),它使用Zebra(ZDesigner LP 2844)驱动程序。 (你可以到这里来:http://www.zebra.com/us/en/support-downloads/desktop/lp-2844.html

我当前的脚本在XP上运行得很好,但在Windows 7上却不太好。它出现了错误 “Microsoft VBScript运行时错误:ActiveX组件无法为我的脚本AddPort.vbs创建对象:'Port.Port.1'

以下脚本称为AddPort.vbs

'ADDING:

dim oPort
dim oMaster
set oPort = CreateObject("Port.Port.1")
set oMaster = CreateObject("PrintMaster.PrintMaster.1")

wscript.echo "Adding port to local machine...."

'Indicate where to add the port. Double quotes ("" ) stand for the local computer, which is the default, or put "\\servername"
oPort.ServerName = ""

'The name of the port cannot be omitted.
oPort.PortName = "CustomPortName"

'The type of the port can be 1 (TCP RAW), 2 (TCP LPR), or 3 (standard local).
oPort.PortType = 3

'For TCP RAW ports. Default is 9100.
oPort.PortNumber = 9101

'Try adding the port.
oMaster.PortAdd oPort

'Test for the status.
If Err <> 0 then
wscript.echo "Error " & Err & " occurred while adding port"
End If

以下脚本称为AddPrinter.vbs 此脚本显示错误“Microsoft VBScript运行时错误:ActiveX组件无法创建对象:PrintMaster.PrintMaster.1

' Adding a Printer
' The sample code in this section creates any required objects, adds a printer to a remote server, and configures some driver and port information. 

dim oMaster
dim oPrinter

wscript.echo "Adding VirtualPrinter printer to local machine...."

'The following code creates the required PrintMaster and Printer objects.
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
set oPrinter = CreateObject("Printer.Printer.1")

'The following code specifies the name of the computer where the printer will be added. To specify the local
'computer, either use empty quotes (“”) for the computer name, or do not use the following line of code. If
'ServerName is not set, the local computer is used. Always precede the name of a remote computer with two backslashes (\\). 
oPrinter.ServerName = ""

'The following code assigns a name to the printer. The string is required and cannot be empty. 
oPrinter.PrinterName = "VirtualPrinter"

'The following code specifies the printer driver to use. The string is required and cannot be empty. 
oPrinter.DriverName  = "ZDesigner LP 2844"

'The following code specifies the printer port to use. The string is required and cannot be empty. 
oPrinter.PortName    = "LoopBack"

'The following code specifies the location of the printer driver. This setting is optional, because by default
'the drivers are picked up from the driver cache directory.
'oPrinter.DriverPath  = "c:\drivers"

'The following code specifies the location of the INF file. This setting is optional, because by default the INF
'file is picked up from the %windir%\inf\ntprint.inf directory.
'oPrinter.InfFile     = "c:\winnt\inf\ntprint.inf"

oPrinter.PrintProcessor = "winprint"

'The following code adds the printer.
oMaster.PrinterAdd oPrinter

'The following code uses the Err object to determine whether the printer was added successfully.
if Err <> 0 then
    wscript.echo "Error " & Err & " occurred while adding VirtualPrinter"
else
    wscript.echo "Printer added successfully"
end if

' To configure other printer settings, such as comments, create a Printer object and then call PrintMaster's method PrinterSet.

wscript.echo "Configuring printer...."

oPrinter.Comment = "Virtual printer to capture labels"
oPrinter.ShareName = "VirtualPrinter"
oPrinter.Shared = true
oPrinter.Local = true

oMaster.PrinterSet oPrinter
if Err <> 0 then
    wscript.echo "Error " & Err & " occurred while changing settings for VirtualPrinter"
end if

还有其他方法可以创建本地打印机,在Windows 7中使用vbscript设置驱动程序,端口号和端口名称以及共享名称和打印处理器???

提前谢谢,最佳回复将获得积分。

2 个答案:

答案 0 :(得分:2)

对于Windows 7,这正是您所需要的:

后面显示的七个打印机实用程序位于C:\Windows\System32\Printing_Admin_Scripts\en-US文件夹中,该文件夹未在路径中列出。因此,您必须实际更改为此文件夹才能运行实用程序。并且,由于这些实用程序旨在从命令行运行,因此您需要从命令提示符窗口启动它们并使用Windows脚本宿主的基于命令行的脚本宿主(Cscript.exe)运行它们。

Windows 7的VBScript打印实用程序

VBScript File   | What it does
---------------------------------------------
Prncnfg.vbs     | Printer configuration
Prndrvr.vbs     | Printer driver configuration
Prnjobs.vbs     | Print job monitoring
Prnmngr.vbs     | Printer management
Prnport.vbs     | Printer port management
Prnqctl.vbs     | Printer queue management
Pubprn.vbs      | Publish a printer to Active Directory

希望这适合你

答案 1 :(得分:0)

我找到了另一种自动添加打印机的方法,首先我创建了一个批处理文件,然后我使用Windows 7附带的prnmngr.vbs为我自动添加打印机。为此,我已经在机器上安装了Zebra驱动程序(但对于任何其他类型的打印机,概念都是相同的)。然后我运行批处理文件。

以下显示我是如何做到的,我在这里添加了批处理文件文本: 将文件命名为:AddPrinter.bat

rem first change the directory to the script location
cd %WINDIR%\System32\Printing_Admin_Scripts\en-US\
rem vbs script path: %WINDIR%\System32\Printing_Admin_Scripts\en-US\Prnmngr.vbs 

rem first add the port, specify name of port, assign it an IP Address, specify the type and the Port.
cscript prnport.vbs -a -r "LoopBack" -h 127.0.0.1 -o raw -n 9100
cscript prnport.vbs -a -r "LoopBack_2" -h 127.0.0.1 -o raw -n 9101
pause

rem specify the name of the new printer, specify the driver, specify the port it will use.
cscript prnmngr.vbs -a -p "VirtualPrinter" -m "ZDesigner LP 2844" -r "LoopBack"
pause

cscript prnmngr.vbs -a -p "Zebra  LP2844" -m "ZDesigner LP 2844" -r "USB0001"
rem cscript prnmngr.vbs -a -p "Zebra  GK420d" -m "ZDesigner GK420d" -r "LPT1:"
rem cscript prnmngr.vbs -a -p "Zebra  GC420d" -m "ZDesigner GC420d" -r "COM1:"
pause

NET STOP SPOOLER
NET START SPOOLER
pause

要自定义此选项,您可以使用这些网站作为参考;

http://technet.microsoft.com/en-us/library/cc725868(v=ws.10).aspx

http://technet.microsoft.com/en-us/library/bb490974.aspx

http://blog.ogwatermelon.com/?p=3489

http://winadminnotes.wordpress.com/2010/02/19/how-to-manage-printers-and-printer-drivers-remotely-from-command-line/

http://technet.microsoft.com/en-us/library/cc754352(v=ws.10).aspx