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

If Then Else 1

Status
Not open for further replies.

JacksonGuy

IS-IT--Management
Dec 18, 2003
38
US
Hello I am trying to memove network printers if they are present so what I think I need to do is do a if a ceritan printer exists then delete it else continue on. My problem is if the printer is not on the machine it hangs and causes long log off times.

Your help is greatly appreated
 
My problem is if the printer is not on the machine it hangs and causes long log off times.
Can you elaborate. Do you mean your script hangs? If so, could you post some code?
 
Ummm Here is the code that I am using.

If IsMember(objcomputer, "Group Name") Then
objNetwork.RemovePrinterConnection "\\servername\sharename"objNetwork.RemovePrinterConnection "\\servername\sharename"
objNetwork.RemovePrinterConnection "\\servername\sharename"
End If

What it does is check group membership of the computer and if it matche it pocedes through the statement (but you prolly figured that out), but what happens is if one of the printers is not installed on the computer then it has to time out in order to go onto the next one. Which cause the slow logoff times.

 
Use the WshNetwork Object to check the EnumeratePrinters collection for the printer before you attempt to disconnect from the printer.
 
TomThumbKP,
That sounds like something that I want to do. How would I do that? Can you point me in the right direction?
 
Something like this maybe (pseudo code):
Code:
If in group then
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  Set oPrinters = WshNetwork.EnumPrinterConnections
  For i = 1 to oPrinters.Count - 1 Step 2
    If oPrinters.Item(i+1) = UNC PATH OF PRINTER TO REMOVE
      DO REMOVAL CODE HERE
  Next
End If
 
TomThumbKP
I have tried what you have suggested and this is what I come up with:
Code:
If IsMember(objcomputer, "Group Name") Then
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  Set oPrinters = WshNetwork.EnumPrinterConnections
  For i = 1 to oPrinters.Count - 1 Step 2
    If oPrinters.Item(i+1) = "\\servername\sharname" Then
      objNetwork.RemovePrinterConnection "\\servername\sharname"
    Next
End If
When I run this I get an Ln 5 Char 7 "Unexpected Next"
Take that out and I don't get an error however nothing works. Is there something that I am missing? Help Please
Thanks
 
I forgot the End If statement right above the next statement. Sorry.
 
TomThumbKP
ok I am getting closer. This is what I have now and I have an error that I don't understand at all. It is on the line
Code:
If oPrinters.Item(i+1) = "\\fro\frolounge" Then 
"Subscript out of range"

Code:
If IsMember(objcomputer, "Frost School") Then
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  Set oPrinters = WshNetwork.EnumPrinterConnections
  For i = 1 to oPrinters.Count - 1 Step 2
    If oPrinters.Item(i+1) = "\\fro\frolounge" Then
      objNetwork.RemovePrinterConnection "\\fro\frolounge"
    End If
  Next
End If
 
The way printer enumeration is supposed to work is that you basically get a collection indexed at 1 where the items are arranged like this:
1) printer 1 name
2) printer 1 unc path
3) printer 2 name
4) printer 2 unc path
.
.
.

I suggest trying to loop through all of the contents of the collection you are getting and printing the value to verify that this is how your collection works.
 
TomThumbKP,
I didn't understand the last post at all. I am a beginner at this, and would like to learn but I may require a little bit more help.
Thanks
 
Simply try this to see what output you get:
Code:
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
For i = 1 to oPrinters.Count - 1
  Wscript.Echo i & ") " & oPrinters.Item(i) 
Next

This way we can see the structure of the collection and try to figure out why we are asking for an item that doesn't exist in the collection. (That is what the 'Subscript out of range' error is telling us)
 
TomThumbKP
ok now that makes sense. I actually had that from another script but wasn't able to put 2 and 2 together. I ran it and I got 1)\\fro\Sharp AR-286 PCL5e (which is the servername\Name of printer) then the next is the 2)Ip address.
 
So the next step would be to pretend you are the computer and go through the loop just as it would and see why it is throwing the error (desktop debugging).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top