none
Windows Error Codes with VBscript RRS feed

  • Question

  • The only thing I can return is basically a "true/false aka a 1 or 0"

    I need the windows error codes such as:
    1787 The security database on the server does not have a computer account for this workstation trust relationship.
    1326 Logon failure: unknown user name or bad password.

    Below is my code:


    <HEAD>
    <TITLE>IT - Cache Credentials</TITLE>
    <HTA:APPLICATION 
    icon="e-icon-new-round.ico"
    ID="InstallApps" 
    BORDER="thin"
    CAPTION="yes"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    SCROLL="yes"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    </HEAD>
    <hta:application icon="e-icon.ico" />

    <script language="VBScript">

    ON ERROR RESUME Next

    Set WshShell = CreateObject("WScript.Shell")
    Set objExplorer = CreateObject ("InternetExplorer.Application")
    Set fso = CreateObject("scripting.filesystemobject")

    '_______________________________________________________________________________________
    'Resize the window when the app starts
    '_______________________________________________________________________________________
    Sub window_onload
    centerWindow
    usrName.focus()
    End Sub

    '_______________________________________________________________________________________
    'Center and size the window
    '_______________________________________________________________________________________
    sub centerWindow
    window.resizeTo 600,675
    window.moveTo (screen.width - 600) / 2, (screen.height - 675) /2
    End sub

    '_______________________________________________________________________________________
    'Closes the application if the Exit button is clicked 
    '_______________________________________________________________________________________ 
    sub killApp
    If fso.FileExists ("c:\Cache Credentials.lnk") Then
    fso.DeleteFile ("c:\Cache Credentials.lnk")
    End If
    window.close
    End sub 

    '_______________________________________________________________________________________
    'Runas command to cache credentials.
    '_______________________________________________________________________________________
    Sub cacheCreds
    On Error Resume Next
    Err.Clear

    userName = usrName.value

    If userName = "" Then
    MsgBox ("Error: Username and password are required")
    Exit Sub
    End If 

    If Right(Left(userName,4),1) = "\" Then
    userName = Mid(userName,5,(Len(userName)))
    usrName.value = userName
    End If 

    runCmd ="runas /profile /user:DOMAIN\" & userName & " notepad"
    retResults = WshShell.Run (runCmd,1,True)

    If retResults <> 0 Then
    MsgBox ("Incorrect password, please try again.")
    usrName.value = ""
    MsgBox "error: " & (retResults)
    Exit Sub 
    End If 

    killApp
    End Sub
    </script>

    <body bgcolor="#f1f2f2">
    <font color = "black">
    <font size ="4" face = "arial"><center><u><b>Cache Domain Credentials<br><br></b></u></font>
    <font size ="2" face = "arial"><b>You must be connected to the internal network either at an office or with VPN before you continue.</b></font size face = "arial"></center></b><br><hr>
    <font size ="2" face = "arial">Enter your domain username and password and then click continue. Do not enter the DOMAIN\ prefix before your username. After you click continue, you will be prompted again for your password.<br><br></font>
    <font size ="2" face = "arial"><left>Domain Username: <br>
    <input type="textbox" name="usrName"><br>
    <br>
    <input type="image" src="continue.gif" name="next_event" onClick="cacheCreds">
    <input type="image" src="cancel.gif" name="run_button" onClick="killApp">
    </font>
    </body>
    Wednesday, July 10, 2019 12:52 PM

Answers

  • Your problem is that you are presuming that the runas command returns the same exit code that it outputs as a text string. That is not the case.

    I tested, and it seems that, for some reason, it isn't possible to capture or redirect the output of the runas command (other than the password prompt).

    All you know is that if the runas command returns a non-zero exit code that something went wrong. But there's no way to know what happened without looking at the message in the console.

    As an alternative, you can use the Sysinternals Shellrunas utility which provides a GUI alternative. This is probably preferable in your use case, I would think.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, July 10, 2019 3:29 PM
    Moderator

All replies

  • I think, "err.number" will be your friend.

    Take a look at https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.errobject.number?view=netframework-4.8

    Kind regards

    Peter

    Wednesday, July 10, 2019 2:27 PM
  • So I changed the code to include err.number however it is still returning only 0 or 1

        If retResults <> 0 Then
            'MsgBox ("Incorrect password, please try again.")
            usrName.value = ""
            'MsgBox "error: " & (retResults)
            MsgBox err.number
            ExitSub
        EndIf

    I fell like it's still not returning the windows error codes.
    I am not sure what I am doing wrong. This is driving me crazy.

    Wednesday, July 10, 2019 3:01 PM
  • Your problem is that you are presuming that the runas command returns the same exit code that it outputs as a text string. That is not the case.

    I tested, and it seems that, for some reason, it isn't possible to capture or redirect the output of the runas command (other than the password prompt).

    All you know is that if the runas command returns a non-zero exit code that something went wrong. But there's no way to know what happened without looking at the message in the console.

    As an alternative, you can use the Sysinternals Shellrunas utility which provides a GUI alternative. This is probably preferable in your use case, I would think.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, July 10, 2019 3:29 PM
    Moderator