VBS network printers and if statement

Home Forums Scripting Windows Script Host VBS network printers and if statement

This topic contains 7 replies, has 4 voices, and was last updated by Avatar shades 7 years, 3 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • Avatar
    shades
    Member
    #159020

    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 “\serverHPLaserJet”

    WshNetwork.SetDefaultPrinter “\serverHPLaserJet”

    WshNetwork.AddWindowsPrinterConnection “\server2Samsung”

    WshNetwork.AddWindowsPrinterConnection “\serverCanon”

    WshNetwork.AddWindowsPrinterConnection “\serverBrother”

    if [“%USERNAME%” = “accountant”]
    then
    WshNetwork.AddWindowsPrinterConnection “\server3HP LaserJet 1100 (MS)”
    wshNetwork.SetDefaultPrinter “\server3HP LaserJet 1100 (MS)”
    else
    end if

    wscript.quit

    Avatar
    Ossian
    Moderator
    #186756

    Re: VBS network printers and if statement

    Can you give us a clue what goes wrong?

    Avatar
    shades
    Member
    #352527

    Re: VBS network printers and if statement

    I get an error:

    Script: \servernetlogonprinter.vbs
    Line: 18
    Char: 33
    Error: Expected ‘Then’
    Code: 800A03F9
    Source: Microsoft VBScript compilation error

    Line 18 is where the if statement begins.

    Avatar
    shades
    Member
    #352528

    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 “\serverHPLaserJet”

    WshNetwork.SetDefaultPrinter “\serverHPLaserJet”

    WshNetwork.AddWindowsPrinterConnection “\server2Samsung”

    WshNetwork.AddWindowsPrinterConnection “\serverCanon”

    WshNetwork.AddWindowsPrinterConnection “\serverBrother”

    if “%USERNAME%” = “accountant” then
    WshNetwork.AddWindowsPrinterConnection “\server3HP LaserJet 1100 (MS)”
    wshNetwork.SetDefaultPrinter “\server3HP 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?

    Avatar
    wullieb1
    Moderator
    #243708

    Re: VBS network printers and if statement

    shades;260131 wrote:
    I got it working!

    On Error Resume Next

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

    WshNetwork.AddWindowsPrinterConnection “\serverHPLaserJet”

    WshNetwork.SetDefaultPrinter “\serverHPLaserJet”

    WshNetwork.AddWindowsPrinterConnection “\server2Samsung”

    WshNetwork.AddWindowsPrinterConnection “\serverCanon”

    WshNetwork.AddWindowsPrinterConnection “\serverBrother”

    if “%USERNAME%” = “accountant” then
    WshNetwork.AddWindowsPrinterConnection “\server3HP LaserJet 1100 (MS)”
    wshNetwork.SetDefaultPrinter “\server3HP 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 “\server3HP LaserJet 1100 (MS)”

    to this

    WshNetwork.SetDefaultPrinter “\server3HP 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

    Avatar
    shades
    Member
    #352529

    Re: VBS network printers and if statement

    Thank you for pointing me in the right direction :smile:

    This is my final script that works great!

    On Error Resume Next

    Set WshNetwork = CreateObject(“WScript.Network”)

    WshNetwork.AddWindowsPrinterConnection “\serverHPLaserJet”
    WshNetwork.SetDefaultPrinter “\serverHPLaserJet”
    WshNetwork.AddWindowsPrinterConnection “\serverSamsung”
    WshNetwork.AddWindowsPrinterConnection “\serverCanon”
    WshNetwork.AddWindowsPrinterConnection “\serverBrother”

    Select Case WshNetwork.UserName

    Case “bob”

    WshNetwork.AddWindowsPrinterConnection “\server1HP LaserJet 5P”
    WshNetwork.SetDefaultPrinter “\server1HP LaserJet 5P”

    Case “accountant”

    WshNetwork.AddWindowsPrinterConnection “\server2HP LaserJet 1100”
    wshNetwork.SetDefaultPrinter “\server2HP LaserJet 1100”

    End Select
    wscript.quit

    Avatar
    wullieb1
    Moderator
    #243723

    Re: VBS network printers and if statement

    Excellent.

    Thanks for posting back.

    Avatar
    Dumber
    Participant
    #201899

    Re: VBS network printers and if statement

    Never use On Error Resume Next if you don’t really need it.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.