Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vb script to detect if printer is installed and then install it from a list if not? (2003 server vbs)

1,901 views
Skip to first unread message

markm75

unread,
Dec 30, 2007, 11:19:15 PM12/30/07
to
This is probably pretty simple.. but i forget the syntax i would need
to do this.. i think i would need to put the list of printers in an
array then scan the array via a loop as i check each installed printer
to see if its one of the ones in the list.. then install it at the end
of the loop if the printer wasnt found as a match to the array list..

Here is the code i currently use.. as it stands it installs the
printers every time a person logs in.. as i mentioned.. i want to put
a blurb of code that will check if the printer is installed (from a
list of the printers) and install only if not found..

Any thoughts on that code that would be needed?:


i think objPrinter.DeviceID would have to not match any of the
possible printers..


-------------------
Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\vsdc02\hplj2200"
WshNetwork.AddWindowsPrinterConnection "\\vsdc02\Phaser"
WshNetwork.AddWindowsPrinterConnection "\\vsdc02\hplj5m"
WshNetwork.AddWindowsPrinterConnection "\\vsdc02\Dell5110"
WshNetwork.AddWindowsPrinterConnection "\\vsdc02\CanonCopier-Fax"
WshNetwork.AddWindowsPrinterConnection "\\vsdc02\CanonCopier-Printer"

WshNetwork.AddWindowsPrinterConnection "\\vsdc01\dell5110"
WshNetwork.AddWindowsPrinterConnection "\\vsdc01\hplj2200"
WshNetwork.AddWindowsPrinterConnection "\\vsdc01\Phaser"
WshNetwork.AddWindowsPrinterConnection "\\vsdc01\hplj5m"
WshNetwork.AddWindowsPrinterConnection "\\vsdc01\CanonCopier-Fax"
WshNetwork.AddWindowsPrinterConnection "\\vsdc01\CanonCopier-Printer"


REM ==== Code to remove stale printers =======
strComputer = "."
strOldServer1 = "\\PSTDC"
strOldServer2 = "\\PSTDC2"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root
\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = False")

If colPrinters.Count <> 0 Then
For Each objPrinter In colPrinters
If UCase(objPrinter.ServerName) = UCase(strOldServer1) Then
WshNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
If UCase(objPrinter.ServerName) = UCase(strOldServer2) Then
WshNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
rem If Ucase(objPrinter.DeviceID) = UCase("\\vsdc02\hplj2200")
Next
End If
REM ======= END CODE TO REMOVE old printers ============

------------------------

markm75

unread,
Jan 3, 2008, 11:35:46 AM1/3/08
to
Others may find this useful.. after significant trial and error.. i
found the looping that works.. here is the code.. Enjoy!:

'========= Code to install printers, only if installed!============

'set the array size 1 less than total number (for VB)
'example servers with share names below
Dim arrayPossPrinters(11)
arrayPossPrinters(0) = "\\server2\hplj2200"
arrayPossPrinters(1) = "\\server2\Phaser"
arrayPossPrinters(2) = "\\server2\hplj5m"
arrayPossPrinters(3) = "\\server2\Dell5110"
arrayPossPrinters(4) = "\\server2\CanonCopier-Fax"
arrayPossPrinters(5) = "\\server2\CanonCopier-Printer"
arrayPossPrinters(6) = "\\server1\dell5110"
arrayPossPrinters(7) = "\\server1\hplj2200"
arrayPossPrinters(8) = "\\server1\Phaser"
arrayPossPrinters(9) = "\\server1\hplj5m"
arrayPossPrinters(10) = "\\server1\CanonCopier-Fax"
arrayPossPrinters(11) = "\\server1\CanonCopier-Printer"


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")


Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = False")

' ====start checking installed printers =====


If colPrinters.Count <> 0 Then

for each objPossPrinter In arrayPossPrinters

bFoundMatch = -1

'check the current installedprinter in the array vs the possible
array list

For Each objPrinterInstalled In colPrinters
'.ShareName is the share ' servername for server
'.DeviceID is the "name" listed in the folder on the server (not
same as sharename)

'**we loop through all possible printers and compare to
the current one in the loop of installed ones
If Ucase(objPrinterInstalled.ServerName & "\" &
objPrinterInstalled.ShareName) = Ucase(objPossPrinter)

Then
bFoundMatch = 1
'break out inner loop
Exit For
End If
Next 'for each obj in colprinters loop

If bFoundMatch = -1 Then
'install printer using objPossPrinter
WshNetwork.AddWindowsPrinterConnection objPossPrinter
End If
' wscript.echo bFoundMatch
Next

End If


'========== end code to install prints only if
installed==================================================

markm75

unread,
Jan 3, 2008, 3:22:23 PM1/3/08
to


Revised below.. forgot to include the install all option if the count
was zero:


If colPrinters.Count <> 0 Then


bFoundMatch = -1


else 'must have this in case there are none installed locally!

WshNetwork.AddWindowsPrinterConnection "\\server2\hplj2200"
WshNetwork.AddWindowsPrinterConnection "\\server2\Phaser"
WshNetwork.AddWindowsPrinterConnection "\\server2\hplj5m"
WshNetwork.AddWindowsPrinterConnection "\\server2\Dell5110"
WshNetwork.AddWindowsPrinterConnection "\\server2\CanonCopier-Fax"
WshNetwork.AddWindowsPrinterConnection "\\server2\CanonCopier-Printer"
WshNetwork.AddWindowsPrinterConnection "\\server1\dell5110"
WshNetwork.AddWindowsPrinterConnection "\\server1\hplj2200"
WshNetwork.AddWindowsPrinterConnection "\\server1\Phaser"
WshNetwork.AddWindowsPrinterConnection "\\server1\hplj5m"
WshNetwork.AddWindowsPrinterConnection "\\server1\CanonCopier-Fax"
WshNetwork.AddWindowsPrinterConnection "\\server1\CanonCopier-Printer"

0 new messages