Announcement

Collapse
No announcement yet.

VBS network printers and if statement

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS network printers and if statement

    Hi Everyone,

    I need some help please with the following script. I'm trying to map shared printers (this is working) but make certain printers default for certain users.

    My script:

    On Error Resume Next

    Set WshNetwork = CreateObject("WScript.Network")

    WshNetwork.AddWindowsPrinterConnection "\\server\HPLaserJet"

    WshNetwork.SetDefaultPrinter "\\server\HPLaserJet"

    WshNetwork.AddWindowsPrinterConnection "\\server2\Samsung"

    WshNetwork.AddWindowsPrinterConnection "\\server\Canon"

    WshNetwork.AddWindowsPrinterConnection "\\server\Brother"


    if ["%USERNAME%" = "accountant"]
    then
    WshNetwork.AddWindowsPrinterConnection "\\server3\HP LaserJet 1100 (MS)"
    wshNetwork.SetDefaultPrinter "\\server3\HP LaserJet 1100 (MS)"
    else
    end if

    wscript.quit

  • #2
    Re: VBS network printers and if statement

    Can you give us a clue what goes wrong?
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: VBS network printers and if statement

      I get an error:

      Script: \\server\netlogon\printer.vbs
      Line: 18
      Char: 33
      Error: Expected 'Then'
      Code: 800A03F9
      Source: Microsoft VBScript compilation error

      Line 18 is where the if statement begins.

      Comment


      • #4
        Re: VBS network printers and if statement

        I got it working!

        On Error Resume Next

        Set WshNetwork = CreateObject("WScript.Network")
        Set objShell = CreateObject("Wscript.Shell")

        WshNetwork.AddWindowsPrinterConnection "\\server\HPLaserJet"

        WshNetwork.SetDefaultPrinter "\\server\HPLaserJet"

        WshNetwork.AddWindowsPrinterConnection "\\server2\Samsung"

        WshNetwork.AddWindowsPrinterConnection "\\server\Canon"

        WshNetwork.AddWindowsPrinterConnection "\\server\Brother"

        if "%USERNAME%" = "accountant" then
        WshNetwork.AddWindowsPrinterConnection "\\server3\HP LaserJet 1100 (MS)"
        wshNetwork.SetDefaultPrinter "\\server3\HP LaserJet 1100 (MS)"
        end if

        wscript.quit



        The printer is being added successfully for the user "accountant" but it is not making it the default printer yet. Any ideas?
        Last edited by shades; 13th June 2012, 20:42.

        Comment


        • #5
          Re: VBS network printers and if statement

          Originally posted by shades View Post
          I got it working!

          On Error Resume Next

          Set WshNetwork = CreateObject("WScript.Network")
          Set objShell = CreateObject("Wscript.Shell")

          WshNetwork.AddWindowsPrinterConnection "\\server\HPLaserJet"

          WshNetwork.SetDefaultPrinter "\\server\HPLaserJet"

          WshNetwork.AddWindowsPrinterConnection "\\server2\Samsung"

          WshNetwork.AddWindowsPrinterConnection "\\server\Canon"

          WshNetwork.AddWindowsPrinterConnection "\\server\Brother"

          if "%USERNAME%" = "accountant" then
          WshNetwork.AddWindowsPrinterConnection "\\server3\HP LaserJet 1100 (MS)"
          wshNetwork.SetDefaultPrinter "\\server3\HP LaserJet 1100 (MS)"
          end if

          wscript.quit



          The printer is being added successfully for the user "accountant" but it is not making it the default printer yet. Any ideas?
          Looks right to me. However just for completeness i would change this

          wshNetwork.SetDefaultPrinter "\\server3\HP LaserJet 1100 (MS)"

          to this

          WshNetwork.SetDefaultPrinter "\\server3\HP LaserJet 1100 (MS)"

          This is a link to the MS article that will show you how to do it.

          http://support.microsoft.com/kb/263226

          Comment


          • #6
            Re: VBS network printers and if statement

            Thank you for pointing me in the right direction

            This is my final script that works great!

            On Error Resume Next

            Set WshNetwork = CreateObject("WScript.Network")

            WshNetwork.AddWindowsPrinterConnection "\\server\HPLaserJet"
            WshNetwork.SetDefaultPrinter "\\server\HPLaserJet"
            WshNetwork.AddWindowsPrinterConnection "\\server\Samsung"
            WshNetwork.AddWindowsPrinterConnection "\\server\Canon"
            WshNetwork.AddWindowsPrinterConnection "\\server\Brother"

            Select Case WshNetwork.UserName

            Case "bob"

            WshNetwork.AddWindowsPrinterConnection "\\server1\HP LaserJet 5P"
            WshNetwork.SetDefaultPrinter "\\server1\HP LaserJet 5P"


            Case "accountant"

            WshNetwork.AddWindowsPrinterConnection "\\server2\HP LaserJet 1100"
            wshNetwork.SetDefaultPrinter "\\server2\HP LaserJet 1100"

            End Select
            wscript.quit
            Last edited by shades; 14th June 2012, 14:30.

            Comment


            • #7
              Re: VBS network printers and if statement

              Excellent.

              Thanks for posting back.

              Comment


              • #8
                Re: VBS network printers and if statement

                Never use On Error Resume Next if you don't really need it.
                Marcel
                Technical Consultant
                Netherlands
                http://www.phetios.com
                http://blog.nessus.nl

                MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
                "No matter how secure, there is always the human factor."

                "Enjoy life today, tomorrow may never come."
                "If you're going through hell, keep going. ~Winston Churchill"

                Comment

                Working...
                X