OK here it is please note I hacked it up because I have more than 1 printer on the network. But I thought showing all of them would get confusing. So here you go. :beer: I am by no means a vbscript expert if you see something that could be done another way please let me know
The example is a HP laserjet 4100 with a Postscript Driver. The IP address of the printer is 192.168.1.200
'*******************
On Error Resume Next
Dim WshNetwork
Dim WshShell
Set WSHNetwork = WScript.CreateObject("WScript.Network")
'========= Register prnadmin.dll file on client computer ============
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run "net use x: \\server\share"
WshShell.Run "xcopy x:\prnadmin.dll %systemroot%\system32\ /D /R /Y",0,true
WshShell.Run "regsvr32 /s %systemroot%\system32\prnadmin.dll",0,TRUE
'====================================================================
'= Adding the printer drivers =
'====================================================================
'========= Check for printer driver HP 4100 PS ============
sKeyPathHP4100 = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\HP LaserJet 4100 PS"
If RegKeyExists(sKeyPathHP4100) Then
'WScript.Echo "Key exists"
Else
'WScript.Echo "Key do not exist"
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run "rundll32 printui.dll,PrintUIEntry /ia /m ""HP LaserJet 4100 PS"" /h ""Intel"" /v ""Windows 2000"" /f x:\Drivers\hp4100ps\hp4100ps.inf /u" , 2 , true
End If
'====================================================================
'= Check for installed printers =
'====================================================================
Dim myPort_192_168_1_200
myPort_192_168_1_200 = 0
strComputer = "."
Set oSvc = GetObject("winmgmts:\\" & strComputer )
Set Printers = oSvc.Get("win32_printer").Instances_
default = false
for each printer in Printers
if printer.portname = "IP_192.168.1.200" then
myPort_192_168_1_200 = 1
else
'MsgBox "else:" &printer.name ,ok
End If
next
if myPort_192_168_1_200 <> 1 then
'====================================================================
'= HP4100 = 192.168.1.200 =
'====================================================================
'************** Create the port *************************************
dim oPort
dim oMaster
Set oPort = CreateObject("Port.Port.1")
Set oMaster = CreateObject("PrintMaster.PrintMaster.1")
oPort.PortName = "IP_192.168.1.200"
oPort.PortType = 1
oPort.HostAddress ="192.168.1.200"
oMaster.PortAdd oPort
if ErrHP4100 <> 0 then
msgbox "There was an error creating the port. 200"
end if
'********************************************************************
'************** Create the printer **********************************
dim oPrinter
Set oPrinter = CreateObject("Printer.Printer.1")
oPrinter.PrinterName = "HP4100" ' name of the printer as it appears in the Printers folder
oPrinter.DriverName = "HP LaserJet 4100 PS" ' name that is referenced in ntprint.inf or specific driver
oPrinter.PortName = "IP_192.168.1.200" ' Specify a port name. Can also point to LPT or COM port.
oMaster.PrinterAdd oPrinter
If ErrHP4100 <> 0 then
msgbox "There was an error creating the printer. 200"
end if
'********************************************************************
End If
WshShell.Run "net use x: /delete"
' --- Subroutine Check Registry for printer driver key-----------------
Function RegKeyExists(sRegKey)
Set oShell = CreateObject("WScript.Shell")
RegKeyExists = True
sRegKey = Trim (sRegKey)
If Not Right(sRegKey, 1) = "\" Then
sRegKey = sRegKey & "\"
End if
On Error Resume Next
RegReadReturn = oShell.RegRead(sRegKey)
If Err Then
If LCase(Left(err.description,7)) = "invalid" Then
'key not found...
RegKeyExists = False
End if
Err.clear
End if
On Error Goto 0
End Function