Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Check if printer exists in collection/array

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61217
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Check if printer exists in collection/array
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Check if printer exists in collection/array - 6/9/2008 4:14:01 AM   
  sharapov

 

Posts: 5
Score: 0
Joined: 6/9/2008
Status: offline
Hello,
 
I am trying to figure out whether one of the printers from the print server had been mapped already using code below, but I am not having too much luck. Can you help?
NOTE: I don’t want to use “For Each objPrinter in colPrinters” statement because I don’t want to iterate through every record in the collection. I just want to find out if a particular printer exists in colPrinters. If it does I don’t want to do anything, if it doesn’t I want to map it.
 
On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")
Dim colInstalledPrinters
Dim objPrinter
Dim strMapPrinter1
Dim strMapPrinter2
Dim strMapPrinter3
Dim strPrintServer
strPrintServer = "\\caprint01\"
strMapPrinter1 = "TOSHIBA e-STUDIO 550 PCL6"
strMapPrinter2 = "TOSHIBA e-STUDIO 520 PCL6"
strMapPrinter3 = "Dell Laser Printer 3100cn PCL6"

strComputerName = "."

Set objWMIService = GetObject( _
    "winmgmts:" & "{impersonationLevel=impersonate}!\\" _
    & strComputerName & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer")
Dim i
For i=0 To UBound(colInstalledPrinters) Step 1
 If colInstalledPrinters(i) = strMapPrinter1 Then
       Wscript.Echo "Printer Already Mapped"
 
 Else
   objNetwork.AddWindowsPrinterConnection strPrintServer & strMapPrinter1
   'objNetwork.SetDefaultPrinter strPrintServer & strMapPrinter1
       End If
   Next
   InArray=False

Set objNetwork = Nothing
Set objWMIService = Nothing
Set colInstalledPrinters = Nothing
WScript.Quit

 
Thank you.
Igor
 
 
Post #: 1
 
 RE: Check if printer exists in collection/array - 6/9/2008 7:45:09 AM   
  dm_4ever


Posts: 2639
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Modify your query...

SELECT * FROM Win32_Printer WHERE DeviceID Like '\\\\Server\\PrinterName'"

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sharapov)
 
 
Post #: 2
 
 RE: Check if printer exists in collection/array - 6/9/2008 8:09:30 AM   
  sharapov

 

Posts: 5
Score: 0
Joined: 6/9/2008
Status: offline
I have over 50 printers. I don't want to do this for every single one. Is there other way?

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Check if printer exists in collection/array - 6/9/2008 8:47:53 AM   
  ehvbs

 

Posts: 2169
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi sharapov,

for one printer dm_4ever's WSQL statement would be executed just once - so
I don't understand your objection.

Regards

ehvbs

(in reply to sharapov)
 
 
Post #: 4
 
 RE: Check if printer exists in collection/array - 6/9/2008 9:56:34 AM   
  sharapov

 

Posts: 5
Score: 0
Joined: 6/9/2008
Status: offline
ehvbs,

Code that I posted is a little snipet of the big logonscript that among other things map printers for users based on security groups membership, as well as user location on the floor. I have over 50 network printers that constantly changing, so what I am trying to do is minimize my administration tasks. I guess dm_4ever's suggestion will work, but I was hoping somebody look at my code and tell me why my method doesn't work. WHat I was ultimately tried to do with my code is create "InArray" type of function that would look for a particular printer in a collection.

Thanks
Sharapov

(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Check if printer exists in collection/array - 6/9/2008 1:55:18 PM   
  dm_4ever


Posts: 2639
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You lost me now....so you want to check if printers exists without looping through a collection?

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sharapov)
 
 
Post #: 6
 
 RE: Check if printer exists in collection/array - 6/10/2008 2:57:01 AM   
  sharapov

 

Posts: 5
Score: 0
Joined: 6/9/2008
Status: offline
dm_4ever

As I mentioned in my initial post, I don't want to do "For Each objPrinter in colInstalledPrinters" and loop through the collection. All I want to do is to check wheather particular printer exists in "colInstalledPrinters" collection. Somethging similar to:

InArray(\\\\server\\printer, colInstalledPrinters)

There is no InArray funcion in vbscript, so I was wondering if there is another way that I can accoplish this.


Sharapov

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: Check if printer exists in collection/array - 6/10/2008 4:12:49 AM   
  dm_4ever


Posts: 2639
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Well you either search for the existence of that specific printer or you loop through all the printers...add them to a dictionary and use its .Exists method to see if your printer exists in the dictionary....or add the printers you're looking for to the dictionary and see if they are part of the collection as you loop through it.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sharapov)
 
 
Post #: 8
 
 RE: Check if printer exists in collection/array - 6/10/2008 8:48:17 AM   
  sharapov

 

Posts: 5
Score: 0
Joined: 6/9/2008
Status: offline
dm_4ever,
 
Can you give me an example how to do it?

Thanks
Sharapov

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: Check if printer exists in collection/array - 6/10/2008 11:30:26 AM   
  dm_4ever


Posts: 2639
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
It just so happens I remembered writing something similar to this a while back....I'll have to see if I included it in the frequently asked stuff post (EDIT:  wasn't...now it is)



      

< Message edited by dm_4ever -- 6/10/2008 11:32:59 AM >


_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sharapov)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Check if printer exists in collection/array Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts