none
Meaning of return codes from WScript.Shell.Run

    Question

  • Hi Guys,

    I am trying to run WBAdmin from JScript with the Windows Scripting Host.

    r = Wscript.Shell.Run("WBAdmin ...", 0, true);

    r is either -1 or -2.  I have concluded that -1 means something is syntactically wrong with the command, so I have to assume that WBAdmin is returning these values and this is probably not really a scripting question at all.

    However, if I get a -2 but then I run the command from a command prompt, it works, which leads me to believe that I am fighting an environmental problem.

    How do  I go about resolving these mysteries?


    Thanks for the help,

    Chris.


    • Edited by cjm51213 Monday, September 3, 2012 9:55 PM
    Monday, September 3, 2012 9:55 PM

Answers

  • I ran


    wbadmin get status

    in an elevated command window and I get a -2 exit code and the message "ERROR - No backup or recovery operation is currently running." So I don't think -2 means you don't have permission. The first thing I would find out is if these exit codes are documented somewhere.

    In any case, as you noted, this is not a scripting issue.

    Bill

    Tuesday, September 4, 2012 10:16 PM
    Moderator

All replies

  • There is no such command in JScript or VBScript.

    You need to "new ActuveXObject" the object.


    ¯\_(ツ)_/¯

    Monday, September 3, 2012 10:15 PM
  • Start here and get this to work before you try anything else.

            var shell = new ActiveXObject("Wscript.Shell");
            var r = shell.Run("notepad", 1, true);


    ¯\_(ツ)_/¯

    Monday, September 3, 2012 10:17 PM
  • Hi jrv,

    Forgive my shorthand, but I am doing the correct thing.  If I were doing what you imply, I would be getting a JScript run time error "Wscript.Shell is null or is not an object." and not an unexplained return code from the run command, right?

    Can you offer any insight to the stated question?


    Thanks for the help,

    Chris.


    • Edited by cjm51213 Monday, September 3, 2012 10:46 PM
    Monday, September 3, 2012 10:36 PM
  • No matter what you are getting the code you posted is wrong that is all.

    Change the code and see what errors you get.


    ¯\_(ツ)_/¯

    Monday, September 3, 2012 10:46 PM
  • Hi jrv,

    Yes the code I posted was shorthand.  The code I am running is doing the correct thing as evidenced by the lack of a JScript runtime error.  So to stop this nonsense, this is what I am really doing:

    cmd = new ActiveXObject("Wscript.Shell");
    r = cmd.Run("WBAdmin ...", 0, true);

    O.K., now, do you have any insight into the return codes of -1 and -2?

    Thanks for the help,

    Chris.

    Monday, September 3, 2012 10:58 PM
  • The first step is to read the documentation:

    http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx

    So we know that r is the return code from the program. If you are getting a return code of -1 or -2, the likely interpretation is that JScript is interpreting r as a signed rather than as an unsigned integer.

    To return r as an unsigned hex string value, you can use a function like this:


    function hex(n) {
      return n < 0 ? (n + Math.pow(2, 32)).toString(0x10) : n.toString(0x10);
    }
    

    Bill

    Monday, September 3, 2012 11:24 PM
    Moderator
  • Hi Bill,

    Yep... The first step is always read the docs and when they are silent, the second step is to ask your friends.  The docs don't give any details of the status code from the "run" command; I've looked.  If I missed it, then I would be most grateful if you would show me where it is.  Or you could even cut the suspense and tell me what -1 and -2 mean?

    The radix of the status code is not very important if I don't have any explanations of any status codes.  And, I think we know that -1 = 0xFF (255) as an 8-bit integer, 0xFFFF (65,535) as a 16-bit integer, and 0xFFFFFFFF (4,294,967,295) as a 32-bit integer, so that translation doesn't help me much.

    Any more thoughts?


    Thanks for the help,

    Chris.

    Monday, September 3, 2012 11:42 PM
  • Hi,

    The docs for WshShell.Run specifically states:

    If [the bWaitOnReturn parameter to function is] set to true, script execution halts until the program finishes, and Run returns any error code returned by the program.

    So we know that in your case, r is the exit code from wbadmin.exe. The interpretation of that error code depends entirely on that program. In Win32, an exit code is a 32-bit unsigned integer (DWORD). Thus, -1 is 0xFFFFFFFF, and -2 is 0xFFFFFFFE. To find out what those exit codes mean, you would need to look up the documentation for wbadmin.exe.

    Bill

    Monday, September 3, 2012 11:52 PM
    Moderator
  • What about this:

    Wscript.Shell.Run

    is not a legitimate jscript line of code unless you have created a class that wraps the WScript object.  In that case teh return code may not be what you expect.

    If you use the correct or at least usab;e ActiveX activation then teh returncode would be meaningful.

    If you insist on a custom wrapper then perhaps you can post the actual code that is being used.

    Almost all programs return positive result codes or negative values that map to hex values between 0x80000001 ox8007FFFF.  I have never seen or heard of an exception or error code that is higher than that.

    See the Microsft documetation on system and application error generation in the SDK.

    -1 and -2 are not Windows system error codes ever.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, September 4, 2012 12:19 AM
    Tuesday, September 4, 2012 12:16 AM
  • Hi Bill,

    I was pretty sure that the -1 and -2 were not really scripting issues and I said as much in my original post.  The second paragraph points out the inconsistency of a -2 return from the "run" command and the same command running from a command prompt and running successfully and raises the question of the scripting environment compared to the command prompt environment. That is the real question here.  Why do I get a -2 with a scripting shell but an apparently successful execution when run from a command prompt?  More importantly, how do I go about diagnosing this? 

    After some more consideration, I am willing to bet I have a permission problem and that is what -2 means.  Running from the command prompt, I am told I must be running with administrative permissions, so I am, but I suspect that I am not running my JScript as Administrator.  This is easy to test, and I will test this first thing tomorrow and tell you want I learn.

    Assuming I am right, and I want to run this script from the Task Scheduler, what am I going to have to do?  Is it sufficient to simply run under the Administrative username? Will the script gain the elevated privs it needs or do I need some other magic?


    Thanks for the help,

    Chris.





    • Edited by cjm51213 Tuesday, September 4, 2012 12:56 AM
    Tuesday, September 4, 2012 12:39 AM
  • Hi,

    What I have been trying to say is that WshShell.Run is simply returning the exit code of the program you run. If wbadmin.exe does not return a consistent exit code, then of course you cannot use its exit code to determine whether it ran successfully. You will have to see if you can determine its success or failure using some other technique (such as redirecting its output to a file and then parsing the file). In any case this is not a scripting question. I suggest finding a more appropriate forum for asking about wbadmin.exe and/or looking up the documentation for that program.

    Bill

    Tuesday, September 4, 2012 12:55 AM
    Moderator
  • Bill - WBadmin is WIndows Backup.  It does not return those error codes.  It is a Microsoft utilitiy and it returns standard Microsoft error codes.  -1 and -02 are not MS error codes.

    The line r = Wscript.Shell.Run("WBAdmin ...", 0, true); is not a legitimate jscript line of code.  We cannot tell what is happening without seeing the rest of the script.

    You cannot access teh shell withthat line of code.

    In jscript we would have top also have a "var r" statement.

    Is this in an HTA or and HTML page?.  Is it a WSF file?

    Without more complete information the question cannot be answered.


    ¯\_(ツ)_/¯

    Tuesday, September 4, 2012 1:21 AM
  • It is a Microsoft utilitiy and it returns standard Microsoft error codes. -1 and -02 are not MS error codes.

    Hi,

    I don't use that utility, but there's not a rule that all command-line programs must exit with a valid Win32 error code value; it's merely a very widely followed convention. A program can exit with any exit code it wants, including 0xFFFFFFFF or 0xFFFFFFFE if it wants to do so. The fact that these are not Win32 error codes doesn't mean a program can't use them as exit codes. I don't know what exit codes wbadmin.exe uses as I don't use that program. As I said, that's a question to be asked in a different forum.

    Of course, as you correctly pointed out, all this is useless if the OP is not calling the WshShell object's Run method from JScript correctly in the first place. The calling code has to be valid.

    Bill

    Tuesday, September 4, 2012 2:26 AM
    Moderator
  • Bill - WBAdmin does not exit with those code.

    The jscript construct is syntactically and  functionally incorrect.  Teh value beinfg reported cannot come from that line of coed.

    That line of code cannot execute a run command.  Look at it.  Try to execute it.


    ¯\_(ツ)_/¯

    Tuesday, September 4, 2012 3:29 AM
  • Hi jrv,

    Apparently you missed the earlier post that resolves your complaint:

    Hi jrv,

    Yes the code I posted was shorthand. The code I am running is doing the correct thing as evidenced by the lack of a JScript runtime error. So to stop this nonsense, this is what I am really doing:

    cmd = new ActiveXObject("Wscript.Shell");
    r = cmd.Run("WBAdmin ...", 0, true);

    So, any insight on the real problem?  What is the meaning of the return codes -1 and -2?  I think -1 means CMD.EXE can't even make sense of what is being asked and I am beginning to suspect that -2 means that WBAdmin has encountered a permission problem, because the identical command will run in a command prompt, which is already running at elevated permissions because WBAdmin requires it.


    Thanks for the help,

    Chris.


    • Edited by cjm51213 Tuesday, September 4, 2012 3:42 AM
    Tuesday, September 4, 2012 3:39 AM
  • So you were making up code that we weere supposed to understand.  Why couldn't you post teh real code that was an issue.

    It is impossible to get WBAdmin to return a -1 or -2.  Something wlse in your code is a probelm and it is not possible to knwo what that is because you will not post the eal code.

    As Bill has pointed out, the return values are from the program you are running, hoeever, I think you are doing something else which is causing you to seem to get those results.  Because you will not post the real code there is no way to help you.

    As for permissions.  If you haev an access error it will likely be a return of 5.

    Yes.  You must run the script in an elevated prompt.


    ¯\_(ツ)_/¯

    Tuesday, September 4, 2012 4:12 AM
  • So, any insight on the real problem?  What is the meaning of the return codes -1 and -2?

    As already noted, please post the exact code you are using and the exact results that leads you to believe you are getting those exit codes. Remember that we cannot see your screen.

    Bill

    Tuesday, September 4, 2012 2:08 PM
    Moderator
  • Hi Bill,

    I've done some research.  You'll find this interesting.  First I wanted to factor out scripting and find out what the error codes actually mean.  I ran "WBAdmin get disk[s]" as both a limited user and an administrative user.  Notice "WBAdmin get disk" is a syntax error because the command is plural.  Here's what happened:

    Standard User Elevated Privs
    WBAdmin get disk %errorlevel% = -1 %errorlevel% = -1
    WBAdmin get disks %errorlevel% = -2 %errorlevel% = 0

    So, clearly "-1" is syntax error, and -2 is "You don't have permission."  Try these and confirm my results.

    Next I tried exactly the same with a scripting run command and I got exactly the same results.  So, clearly the problem was privilege elevation.  I was pasting into an elevated command prompt window but running the script from standard privs.  Problem solved.

    I do have another problem, and I'll start another thread for that.


    Thanks for the help,

    Chris.

    Tuesday, September 4, 2012 10:10 PM
  • I ran


    wbadmin get status

    in an elevated command window and I get a -2 exit code and the message "ERROR - No backup or recovery operation is currently running." So I don't think -2 means you don't have permission. The first thing I would find out is if these exit codes are documented somewhere.

    In any case, as you noted, this is not a scripting issue.

    Bill

    Tuesday, September 4, 2012 10:16 PM
    Moderator
  • Hi Bill,

    Yeah,  -1 seems to be unreasonable request -- syntax, file not found, ...   -2 seems to be, "I can't do that, Dave.".


    Thanks for the help,

    Chris.


    • Edited by cjm51213 Tuesday, September 4, 2012 11:15 PM
    Tuesday, September 4, 2012 10:47 PM