+ Post New Thread
Results 1 to 6 of 6
Coding Thread, VBS and Listing printers in Coding and Web Development; Hi All, I am writing a new logon script in VB Script. As part of this I need to check ...
  1. #1

    Join Date
    Sep 2005
    Posts
    16
    Thank Post
    1
    Thanked 2 Times in 2 Posts
    Rep Power
    19

    VBS and Listing printers

    Hi All,

    I am writing a new logon script in VB Script. As part of this I need to check the network printers that the user already has connected, and connect/disconnect printers as requires.

    The following code works very well, however if I run it as a logon script it takes 9/10 seconds to run. If I run it after logon it takes less than a second. As a logon script it seems to spend most of the time doing 'nothing' with a quick burst right at the end.

    Does anyone have any ideas as to why this is? Or any ideas as to how to get the info in some other way without the delay?


    Code:
    'Loop round all printers, only list ones that start \\
    Set WSHPrinters = WSHNetwork.EnumPrinterConnections
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
        If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
    	LogMsg("Found Printer: "&WSHPrinters.Item(LOOP_COUNTER +1))
        End If
    Next
    Many thanks

    Will

  2. #2

    vikpaw's Avatar
    Join Date
    Sep 2006
    Location
    Saudi Arabia
    Posts
    5,970
    Thank Post
    673
    Thanked 1,383 Times in 1,145 Posts
    Rep Power
    350
    I know nothing, so just guessing, but maybe it is running before the printer connections are setup/recognised yet so there is nothing to enumerate. Is there a way you can delay the script until later? Perhaps put in some debugging like once you've set variable to enumprinterconnections, echo the value to screen. if it takes 9 seconds for that to happen, then it might be what i'm thinking. either that or something else is delaying the script.
    like i said i know nothing.

  3. #3

    Join Date
    Sep 2005
    Posts
    16
    Thank Post
    1
    Thanked 2 Times in 2 Posts
    Rep Power
    19
    I have enhaced the code slightly so that it logs every printer found not just the network ones.


    Code:
    'Loop round all printers, only list ones that start \\
    Set WSHPrinters = WSHNetwork.EnumPrinterConnections
    For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
        If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
    	LogMsg("Found Network Printer: "&WSHPrinters.Item(LOOP_COUNTER +1))
        Else
    	LogMsg("Found Local Printer: "&WSHPrinters.Item(LOOP_COUNTER +1))
        End If
    Next
    This gives the following at logon: (printer names replaced by ***)

    Code:
    21/09/2009 15:56:30: Listing all mapped network printers...<br />
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Local Printer: ***
    21/09/2009 15:56:40: Found Network Printer: \\***\***
    21/09/2009 15:56:40: Found Network Printer: \\***\***
    If I run the code after logon there is no delay, or indeed if I run the same code again later on in the script there is no delay.

    If I put a sleep in the script to say wait 2 mins and then list the printers, this makes no difference the script still sits there for 10 seconds.

    It looks like it is actually conncting to the server to verify the printers exist. Could this casue the delay?

  4. #4

    Join Date
    Sep 2005
    Posts
    16
    Thank Post
    1
    Thanked 2 Times in 2 Posts
    Rep Power
    19
    Thanks for all your help. At least I know that the code should work.

    I think I will do some packet logs tommorrow using ethreal to see if I can find out what happens in that time frame.

    Will

  5. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,763
    Thank Post
    3,273
    Thanked 1,053 Times in 974 Posts
    Rep Power
    365
    you could use the win32_printer class from wmi and get all the installed printers that way

    WMI and VBScript to query a printer using a Win32_Class

    Obviously from the above example you can delete all the items you dont require that are between the for next loop

  6. #6

    Join Date
    Sep 2005
    Posts
    16
    Thank Post
    1
    Thanked 2 Times in 2 Posts
    Rep Power
    19
    Well I finally have got to the bottom of this...

    All of my test accounts have "been arround for a while" therefore at some point in the past have connected to printers on servers which no longer exist.

    These connections have been stored in the registry (as they should). When our existing (old script) touches printers it uses con2prt.exe
    It appears con2prt.exe does not properly remove a printer which it is unable to connect to, it removes the "visible bits" but leaves connection settings in the registry.

    If I load the ntuser.dat file into a regedit and remove the old junk then the script progresses as it should.

    Just for reference, if this problem exists you cannot use the WMI method others suggest - that times out so the array cannot be looped.

    Hope this helps someone else out in the future!

    Will

  7. Thanks to wlaidlaw from:

    vikpaw (10th October 2009)

SHARE:
+ Post New Thread

Similar Threads

  1. [Ubuntu] listing files alphabetically
    By RabbieBurns in forum *nix
    Replies: 5
    Last Post: 26th June 2009, 07:40 AM
  2. mapping printers via .vbs & GPO
    By techie211 in forum Windows Server 2000/2003
    Replies: 16
    Last Post: 12th June 2009, 09:46 AM
  3. Disable directory listing
    By beeswax in forum How do you do....it?
    Replies: 8
    Last Post: 22nd May 2008, 08:41 AM
  4. Listing of Vista Drivers
    By Geoff in forum Windows Vista
    Replies: 2
    Last Post: 23rd February 2007, 02:07 PM
  5. Useful(?) to some - OSS listing on the web.
    By indiegirl in forum Educational Software
    Replies: 0
    Last Post: 24th July 2006, 11:50 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •