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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking connected net printers before mapping

Status
Not open for further replies.

puko

IS-IT--Management
Oct 9, 2009
8
US
hello,
We are having trouble keeping printers mapped on student computers. we have login scripts with

' Windows Logon Script for mapping printers.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\UNC\UNC"
Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "\\UNC\UNC"

WScript.Quit

that we use, but if the printers are alredy mapped it will give off an error.

so i was just wanting to make a script that checked for 2 network printers and if they were mapped..it would do nothing..and if they wernt they would get mapped

I found mr movie's code for checkin to see if printers are already mapped but there was some group stuff in there too and im really not that far along with vbs to know what was overlapping.
 
Try something like this:
Code:
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = FALSE")
For Each objPrinter In colPrinters
   strPrinter=objPrinter.Name
   If strPrinter="\\UNC\UNC" Then
Do something....
This ought to get you going.
 
alright.. did a little work and was wondering if this would funtion, i gotta go take a break and was going to come finish..but was wanting to see if it was even feasable



' Windows Logon Script for mapping printers.

option explicit
dim objNetwork
dim objPrinter
dim objDictionary

Set objDictPrnts = CreateObject("Scripting.Dictionary")
objDictPrnts.Add "\\printserv\printer1", "\\printserv\printer1"
objDictPrnts.Add "\\printserv\printer1", "\\printserv\printer2"


If objDictionary.Exists("\\printserv\printer1") Then
' Do something
' make it skip with no prompt(dont know this command)
Else
' Do something else
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\printserv\printer1"

Set objPrinter = CreateObject("WScript.Network")
objPrinter.SetDefaultPrinter "\\printserv\printer1"

End If
next


If objDictionary.Exists("\\printserv\printer2") Then
' Do something
' make it skip with no prompt(dont know this command)
Else
' Do something else
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\printserv\printer2"


End If
 

the
If objDictionary.Exists("\\printserv\printer1") Then



is going to be looking at
Set objDictPrnts = CreateObject("Scripting.Dictionary")
objDictPrnts.Add "\\printserv\printer1", "\\printserv\printer1"
objDictPrnts.Add "\\printserv\printer1", "\\printserv\printer2"

and not the acutal connected printers eh?
 
If you want to make it skip then don't put any command in the IF portion
Code:
If objDictionary.Exists("\\printserv\printer1") Then
Else
   Set objNetwork = CreateObject("WScript.Network")
   objNetwork.AddWindowsPrinterConnection "\\printserv\printer1"
   objNetwork.SetDefaultPrinter "\\printserv\printer1"
End If

or better yet, make a single condition by reversing the IF
Code:
If NOT objDictionary.Exists("\\printserv\printer1") Then
   Set objNetwork = CreateObject("WScript.Network")
   objNetwork.AddWindowsPrinterConnection "\\printserv\printer1"
   objNetwork.SetDefaultPrinter "\\printserv\printer1"
End If

you also have a random "next" after the End If. It shouldn't be there.

---

Yes, the script will only look at the dictionary content, not the actual printer connection. As dm4ever pointed out, you need to enumerate your printer connections and compare them to the dictionary.

if the printer is in the dictionary then
if the printer is not connected then
connect printer

-Geates
 
The dictionary defines what printers you want to be present after your script has executed...you still need to enumerate your currently mapped printers to see what is already there...by using the dictionaries Remove method we modify the content of the dictionary to only contain those printers that are missing...we then loop through those remaining printers and add them

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
i got it going.

i appricate the time for the explinations and helping me through, i would have had a coffee induced nervous breakdown otherwise, thank yall very much



On Error Resume Next

Set WSHNetwork = CreateObject("WScript.Network")

' create a dictionary to store our printer paths
Set objDictPrnts = CreateObject("Scripting.Dictionary")
objDictPrnts.CompareMode = vbTextCompare
objDictPrnts.Add "\\UNC\UNC", "\\UNC\UNC"
objDictPrnts.Add "\\UNC\UNC2", "\\UNC\UNC2"


' loop through printer connections
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
PrinterPath = WSHPrinters.Item(LOOP_COUNTER +1)
' if the current path exist in our dictionary remove it
If objDictPrnts.Exists(PrinterPath) Then objDictPrnts.Remove PrinterPath
Next

' loop through the path's that were not found and add them
For Each PrinterPath In objDictPrnts.Keys
WSHNetwork.AddWindowsPrinterConnection PrinterPath
Next
 
personally i would avoid the use of EnumPrinterConnections as it is criminally slow if the user has a printer mapped which cannot be located, e.g. perhaps it is from their home, a different office, an older shared printer which doesnt exist etc etc. any of the above result in really poor logon times and you will get blamed for it.
in the past i have used the below with success:

'msgbox "init printers"
Dim strRootKey, arrSubKeys, refRegistry, subKey, strPrinterMapping

Set enumPrinters = WScript.CreateObject("Scripting.Dictionary")
Set refRegistry = GetObject("winmgmts:root\default:StdRegProv")
Const HKEY_CURRENT_USER = &H80000001
strRootKey = "Printers\Connections"

If refRegistry.EnumKey(HKEY_CURRENT_USER, strRootKey, arrSubKeys) = 0 Then
'init dictionary object of printers
For Each subKey In arrSubKeys
strPrinterMapping = ""
strPrinterMapping = Trim(LCase(CStr(subKey)))
If InStr(strPrinterMapping, ",,") = 1 Then
strPrinterMapping = Replace(strPrinterMapping, ",", "\")
If enumPrinters.Exists(strPrinterMapping) Then
'already have this one, unlikely
Else
'Wscript.Echo "adding " & strPrinterMapping & " to enumPrinters"
enumPrinters.Add strPrinterMapping, "1"
End If
End If
Next
End If
Set refRegistry = Nothing


 
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
PrinterPath = WSHPrinters.Item(LOOP_COUNTER +1)
' if the current path exist in our dictionary remove it
If objDictPrnts.Exists(PrinterPath) Then objDictPrnts.Remove PrinterPath
Next

why would you want to disconnect the printer only to remap it? (ok there are circumstances where you might want to do this? a new printer driver perhaps?, this would be a special case though) mapping printers take time, deleting a printer only to remap represents a waste of time. even if it only wastes 5 secs per printer, you may only have 3 printers per user = 15 seconds...per day, per user, we have 215,000 users. the idea that i am going to waste 15 * 215,000 man seconds a day for the company = me fired
 
what a stupid last post, please accept my apologies, that is exactly what your code is doing
 
perhaps i can redem myself?

your use of the key and item here..

objDictPrnts.CompareMode = vbTextCompare
objDictPrnts.Add "\\UNC\UNC", "\\UNC\UNC"
objDictPrnts.Add "\\UNC\UNC2", "\\UNC\UNC2"

gives you the chance to assign another property to the printer (personally i think a printer class would be of benefit in the long run), perhaps a True or False to represent if you want to connect the printer when the user is in a remote location? mapping printers over RAS/Dialup/VPN etc can be positively painful. would be nice to have a IsLocal() IsRemote() type of thing going on and then make decisions about what printers/drives/filecopy/logging you want to do based on the type of connection or the location of the user
 
thanks for the enumerate printer work around...i gotta save all the time i can cause 1/3 of the clients are old dells, and through my research i found out that it was called key and item, just not that it could be this useful. i plan on scrounging through the forum for the next few months, adding onto this so we can impliment one solid printer/drive map login script when we get ADS fixed so i -greatly- appriciate the info

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top