Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Printers

Status
Not open for further replies.

JDU

Technical User
Dec 23, 2002
182
US
Currently I use the following script to map printers:
How can I modify it so that the script will look for the shared printers on the server and then map it if it is shared so that I don't have to keep adding printer names to the array. Thanks.

Dim i,j,PrinterPath,Printer,wshnetwork,cPrinters,bFound,arrArray

arrArray=array("ACCH01","ACCH02","ACCH03","BBCH01","CHCH01","ERBHRCH01", "ERCDCCH01","ERCPCCH01", _
"EREMRCH01","ERFSTCH01","EROBGCH01","ERPERCH01","ERRSSCH01","ERTRMCH01","FDCH01", _
"ICCH01","LOCH01","MBCH01","NS1NOCH01","NS1SECH01","NS1SOCH01","NS2SOCH01","NS2WECH01", _
"NS3SOCH01","NS3WECH01","NS4NOCH01","NS4SOCH01","NS5NOCH01","NS5SOCH01","NSBCUCH01","NSCATHCH01", _
"NSCCUCH01","NSCVCUCH01","NSFBCCCH01","NSHROBCH01","NSIMCCH01","NSLDCH01","NSMICU1CH01","NSMICU2CH01", _
"NSNICU1CH01","NSNICU2CH01","NSNSCUCH01","NSONCCH01","NSPACU1CH01","NSPACU2CH01","NSPEDCH01","NSPICUCH01", _
"NSPOVFCH01","NSRRHCH01","NSSICUCH01","NSTICUCH01","NSTPACUCH01","QACH01","QBCH01","QCCH01","QECH01","QHCH01", _
"QMCH01","QNCH01","QPCH01","QRCH01","QSCH01","QUCH01","QVCH01","TRCH01","ONCH01","LOCH02","PHCH01", "MGRCH01", "NSAMBSURCH01", _
"NSTRNSRVCH01", "NSWELLCTRCH01", "NSUWCCH01", "NSPOPCCH01", "NSOPCCH01", "billcodch01", "nsmedrecch01", "nscthlabch01", "nsperimpch01")


set wshnetwork=createobject("wscript.network")
set cPrinters=wshnetwork.EnumPrinterConnections
bFound=false

For j= LBound(arrArray) To UBound(arrArray)
Printer=arrArray(j)
PrinterPath = "\\Pathsrv\" & Printer

for i=0 to cPrinters.count-1 step 2

if strComp(cstr(cPrinters.Item(i+1)), cstr(PrinterPath), 1)=0 then
bFound=true
exit for
end if


next

if Not bFound then
wshnetwork.AddWindowsPrinterConnection PrinterPath
end if

bFound=false

Next

set cPrinters=nothing
set wshnetwork=nothing






 
seeing as you only call

set cPrinters=wshnetwork.EnumPrinterConnections

once, this means that you dont have dynamic reflection of what printers are connected on the machine. thats fine though as i dont think its practical to keep querying enumprinters and providing you dont have dup's in your array it shouoldnt matter anyway. besides enumprinters is rubbish if you have a dead printer on the client, i.e. it points to a printer which isnt available anymore, i try and avoid enum, printers and just read the registry for printers instead.

that was a bit of an aside.

i would recommned that when you call enumprinters you put the results to a dictionary object. that way you can use the dicPrinters.Exists method of the hash table which should prove quicker than looping through every installed printer for every target printer in your array.

For Each aTargetPrinter In dicTargetPrinters
If Not dicInstalledPrinters.Exists(aTargetPrinter) Then
'map the printer
End If
Next

with regards to checking which printers are on a particular server then you should check out ADSI

Set objServer = GetObject("WinNT://domain/servername")
objServer.Filter = Array("PrintQueue")
For Each aPrintQ In objServer
Wscript.Echo PrintQueue.Name
Next


I think it is noble to run through the printers on a server, much more dynamic.
however, if this is for a logonscript then i would advise against it, each user would have to connect to each server on each logon. whilst this keeps things dynamic i am not sure about the speed implications of connecting to all these objects. whilst i dont advocate having them hard coded in your vbscript, i prefer ini files for that purpose. you might consider a central script running once a day which updates the ini files by checking the servers? i know this might sound silly but you might find this is faster, its a toss up i guess.

good luck
 
Thankyou so much for your reply. I am not a programmer, know just a little bit of it. Even though this is a logon script only one user log's on to this machine and it is the same user every day. How can I write a script that will check the shared printers on a particular server and then if it doesn't exist on the client, will automatically map it without any user intervention. Thanks again.
 
'try this, mrmovie

Option Explicit
Dim WshNetwork, cPrinters, dicCurrPrnts, dicTargetPrinters, i, strTemp, aTargetPrinter

Set WshNetwork = CreateObject("Wscript.Network")
Set cPrinters = WshNetwork.EnumPrinterConnections

Set dicCurrPrnts = CreateObject("Scripting.Dictionary")
'get currently installed printers
For i = 0 To cPrinters.Count-1 step 2
strTemp = ""
strTemp = LCase(CStr(cPrinters.Item(i+1)))
If Not dicCurrPrnts.Exists(strTemp) Then
Wscript.Echo "adding current printer = " & strTemp
dicCurrPrnts.Add strTemp, "1"
End If
Next
Wscript.Echo "num of current printers = " & dicCurrPrnts.Count

'get the printers from one server could have array of servers, or just keep calling it
'with different params and the same dic object or more dic objects etc etc
Set dicTargetPrinters = CreateObject("Scripting.Dictionary")
Call getPrinters("RMPLC", "rufus", dicTargetPrinters)
Wscript.Echo "num of printers found = " & dicTargetPrinters.Count

'compare the two dic objects
For Each aTargetPrinter In dicTargetPrinters
If Not dicCurrPrnts.Exists(aTargetPrinter) Then
Wscript.Echo "need to install = " & aTargetPrinter
WshNetwork.AddWindowsPrinterConnection aTargetPrinter
Else
Wscript.Echo "already have installed = " & aTargetPrinter
End If
Next

Set dicTargetPrinters = Nothing
Set dicCurrPrnts = Nothing
Set WshNetwork = Nothing
Set cPrinters = Nothing

Public Sub getPrinters(ByVal strDomain, ByVal strServerName, ByRef dicPassed)
Dim strTemp, objServer, aPrintQ
Set objServer = GetObject("WinNT://" & strDomain & "/" & strServerName)
objServer.Filter = Array("PrintQueue")
For Each aPrintQ In objServer
strTemp = ""
strTemp = LCase(aPrintQ.Name)
If Not dicPassed.Exists("\\" & LCase(strServerName) & "\" & strTemp) Then
Wscript.Echo "adding " & "\\" & LCase(strServerName) & "\" & strTemp
dicPassed.Add "\\" & LCase(strServerName) & "\" & strTemp, "1"
End If
Next
Set objServer = Nothing
End Sub
 
i hate it when i do that, you will need to change the domain and servername to your needs
 
MrMovie I am impressed. Thanks so much. I appreciate your help with this.
 
One question: I don't understand this line
Call getPrinters("RMPLC", "rufus", dicTargetPrinters)

What's RMPLC and rufus.
 
Call getPrinters("yourDOMAIN", "yourServerName", dicTargetPrinters)


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Got it. Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top