none
VB Script to Map Printers Not Running At Login

    Question

  • Hi,

    I have a VB script set in the user section of a GPO that is supposed to map two printers at log in. But, like most Windows technologies, that is not happening. If I were to run the script interactively, no problem, everything is fine. The Event Log contains nothing intelligible, as usual. The printers are online, and I can browse to them through \\SERVER\Printer from the Run dialog.

    Anything else I could try?

    Thanks


    Jason

    Thursday, January 21, 2016 7:23 PM

Answers

  • GPP seems to be working. I didn't write the VBscript. Nor do I have the time to learn how to properly write such a script. I just need to get the printers assigned. The problem with every programming language I've every dealt with is that you have to learn everything to do anything. I just don't have that time. It would take me months to do something like that.

    Thanks for the help everyone!


    Jason

    Monday, January 25, 2016 2:39 PM

All replies

  • Hi Jason,

    Have you checked if the GPO is applied successfully? Please run gpresult /r to take a look.
    If GPO is applied correctly, the issue may be caused by VB script then.


    Please remember to mark the replies as answers if they help and un-mark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, January 22, 2016 1:21 AM
    Moderator
  • Hi,

    I had the GPO create a desktop shortcut in the same section where the printers should be mapped. The shortcut is there, so it stands to be seen that the GPO is applied. GPResult /R shows the GPO as being applied as well. The VB script runs interactively when it is launched from an administrative command prompt.

    This is the script...

    Dim MyValue

    Set WshNetwork = CreateObject("WScript.Network")
    Set WshShell = CreateObject("WScript.Shell")

    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    'Remove only network printers and leave the local printer devices alone
      IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
      PrinterPath = oPrinters.Item(Counter)
      WshNetwork.RemovePrinterConnection PrinterPath, True, True
      END IF
    NEXT

    WshNetwork.AddWindowsPrinterConnection "\\server\ColorPrinter"
    WshNetwork.AddWindowsPrinterConnection "\\server\LaserPrinter"

    Randomize
    MyValue = Rnd

    IF (MyValue < .5) THEN
    WshNetwork.SetDefaultPrinter "\\server\ColorPrinter"
    ELSE
            WshNetwork.SetDefaultPrinter "\\server\LaserPrinter"
    END IF

    Jason

    Friday, January 22, 2016 1:36 AM
  • Hi Jason,

    How do you assign the script to users? Where do you store the script?
    The user might have received the Group Policy successfully, but failed to receive the actual script.

    Besides, I suggest that you could use Group Policy Preferences , it is much easier than running script to deploy printers.


    Please remember to mark the replies as answers if they help and un-mark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, January 22, 2016 8:13 AM
    Moderator
  • "Always wait for the network at startup and logon" is enabled?
     
    > Set oPrinters = WshNetwork.EnumPrinterConnections
     
    As with most windows technologies, if you implement them properly, they
    would work.
     
    If this command fails, your script terminates. So I suggest to use "On
    Error Resume Next" before and "If err ..." after. In addition, your
    script completely lacks any logging. This makes it impossible to track
    down its behavior during logon.
     
    > Randomize
    > MyValue = Rnd
    >
    > IF (MyValue < .5) THEN
    > WshNetwork.SetDefaultPrinter "\\server\ColorPrinter"
    > ELSE
    >          WshNetwork.SetDefaultPrinter "\\server\LaserPrinter"
    > END IF
     
    Funny part :-))
     
    Friday, January 22, 2016 11:39 AM
  • "If this command fails, your script terminates. So I suggest to use "On

    Error Resume Next" before and "If err ..." after" 

    You also need to Dim your variables..... also clear them to close out script MyValue = Nothing WshShell = Nothing.... etc.

    Dim MyValue

    Set WshNetwork = CreateObject("WScript.Network")
    Set WshShell = CreateObject("WScript.Shell")

    Set oPrinters = WshNetwork.EnumPrinterConnections
    FOR Counter = 0 to oPrinters.Count - 1
    'Remove only network printers and leave the local printer devices alone
      IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN
      PrinterPath = oPrinters.Item(Counter)
      WshNetwork.RemovePrinterConnection PrinterPath, True, True
      END IF
    NEXT

    WshNetwork.AddWindowsPrinterConnection "\\server\ColorPrinter"
    WshNetwork.AddWindowsPrinterConnection "\\server\LaserPrinter"

    Randomize
    MyValue = Rnd

    IF (MyValue < .5) THEN
    WshNetwork.SetDefaultPrinter "\\server\ColorPrinter"
    ELSE
            WshNetwork.SetDefaultPrinter "\\server\LaserPrinter"
    END IF

    Friday, January 22, 2016 4:24 PM
  • > You also need to Dim your variables..... also clear them to close out
    > script MyValue = Nothing WshShell = Nothing.... etc.
     
    Dim is only neccesary with "Option Explicit" - although I agree that
    this is best practice to avoid typos in variable names :)
     
    And cleanout is NOT required - WSH will do this on its own when the
    script finishes.
     
    Monday, January 25, 2016 11:39 AM
  • Besides, I suggest that you could use Group Policy Preferences , it is much easier than running script to deploy printers.

    I second this, forget trying to write a script when Group Policy has a built in feature to map printers.

    There's no need to re-invent the wheel

    Monday, January 25, 2016 11:51 AM
  • > I second this, forget trying to write a script when Group Policy has a
    > built in feature to map printers.
     
    I don't - GPP can not handle device settings for local printers, it has
    its quirks when it comes to startup/logon performance (no async
    processing), and it cannot really handle printer assignments that are
    pulled from external data :)
     
    The performance issue is my main reason to discourage GPP and use
    scripts for potentially long running tasks like copying files or
    installing printers (and their drivers, obviously).
     
    Monday, January 25, 2016 11:55 AM
  • GPP seems to be working. I didn't write the VBscript. Nor do I have the time to learn how to properly write such a script. I just need to get the printers assigned. The problem with every programming language I've every dealt with is that you have to learn everything to do anything. I just don't have that time. It would take me months to do something like that.

    Thanks for the help everyone!


    Jason

    Monday, January 25, 2016 2:39 PM