0
votes

i'm writing a script that will set a network printer as a global printer assigned to a computer so when a new user logs in they won't have to manually add the printer it will already be there. I'm using the command rundll32 printui PrintUIEntry. So we'll have techs going around to each computer (being admins on the computer) and they'll run this script. A pop up box will come up and they'll type in the printer name and it ok and it runs the rundll32 command. We have 2 printer servers "vps01" and "vps02" so the command will run twice looking for the printer. I'm having a problem with error handling. I'm not sure where to start. I tried setting up error handling but i know it's wrong. The way i have it the command runs but if i put in a invalid printer name it still says the printer is added. Any help is much appreciated.

    Dim objNet, strPrinter
    Set objNet = CreateObject("Wscript.Network")
    strPrinter = InputBox("Please enter the name of the Printer you'd like to add", "Add Printer", "eg. printer name")
    Set objShell = CreateObject("WScript.Shell")
    arrServers = Array("vps01","vps02")
    For Each strServer In arrServers
        strCommand = "cmd /c rundll32 printui.dll,PrintUIEntry /ga /n\\" & strServer & "\" & strPrinter
        objShell.Run strCommand, 1, True
    next
    If err.number = 0 Then
    Wscript.Echo strPrinter & " added!"
    Else
    Wscript.Echo "Uh oh - there seems to be a problem! Error Code : " & err.number & " Error Description: " & err.description
    End If
1

1 Answers

1
votes

Err.Number will only show you errors from the code or from a COM component, but it looks like you're trying to run a command and then read the output.

I think there's a couple approaches you could take:

  1. If you store the return value from objShell.Run (something like result = objShell.Run(strCommand, 1, True) then you can see if the command returned success. I'm not sure though if you'll just always get a success, since the command you're running is cmd, and it's likely succeeding at running the command you told it to. You may be able to run rundll32 directly rather than running it through cmd. UPDATE: This won't help you. While cmd will pass along the return code from rundll32, it will always be 0 because rundll32 functions always return void.
  2. Instead of using the .Run method, you could use .Exec which allows you to access the output stream. You could parse it for the success and failure messages that you're looking for.
  3. Instead of calling out to the command line to run the command, use WMI or some other object model to add the printer. (Maybe a login script that calls AddWindowsPrinterConnection works well enough for your needs rather than a system network printer connection?) This may be more hassle than it's worth, especially if you're not already familiar with WMI for a quick install script, but it's probably the more correct way to do things, and it would let you handle any errors in a bit more direct way, probably by using the Err.Number technique that it looks like you're trying to do.