Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

VB Script Adding Network Printers

I have a VBS script that I've written that almost does what I want it to. See Below.  Here's my problem. If the user doesn't have one of the specified printers mapped, the script errors out and doesn't finish.  Is there another IF statement or else statement I can add that will check to see if the user has the printer mapped and if not continue with the script removing the printers they do have.

This is what I have:

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

WshNetwork.RemovePrinterConnection "\\servername\tango1"
WshNetwork.RemovePrinterConnection "\\servername\tango2"
WshNetwork.RemovePrinterConnection "\\servername\tango3"
WshNetwork.RemovePrinterConnection "\\servername\tango4"

strCommand = "%comspec% /c P:\IfMember.exe Sales"

intReturn = objShell.Run(strCommand, 2, True)
If intReturn = 1 Then
WshNetwork.AddWindowsPrinterConnection "\\servername\tango1"
WshNetwork.SetDefaultPrinter "\\servername\tango2"
End If

Depending on the line the script stops on this is a typcial error.  

Script:  P:\printer.vbs
Line:    4
Char:   1
error:   This network connection does not exist
1 Solution
Bob LambersonSoftware EngineerCommented:
> Set WshNetwork = CreateObject("WScript.Network")
> Set objShell = CreateObject("Wscript.Shell")

> On Error Resume Next       <<<<<<<<<<<<<<<<<<<<<< add this line here

> WshNetwork.RemovePrinterConnection "\\servername\tango1"
> WshNetwork.RemovePrinterConnection "\\servername\tango2"
> WshNetwork.RemovePrinterConnection "\\servername\tango3"
> WshNetwork.RemovePrinterConnection "\\servername\tango4"

> strCommand = "%comspec% /c P:\IfMember.exe Sales"

> intReturn = objShell.Run(strCommand, 2, True)
> If intReturn = 1 Then
> WshNetwork.AddWindowsPrinterConnection "\\servername\tango1"
> WshNetwork.SetDefaultPrinter "\\servername\tango2"
> End If

swpa_wntAuthor Commented:
That did it .... Thank you very much.


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now