Michael's profileThe Old Dogs Scripting B...BlogListsNetworkMore Tools Help

Blog


    August 07

    Error trapping and EXE's in vbScript

    I was asked to make up a menu for some connection testing.
    Many of the machines that are being tested are Windows XP and many of the testers
    are using XP.
     
    I found that Net View works differently on XP than it does on Vista,
    So I had to come up with a way to determine if the C$ share was available
    on either XP or Vista machines and from either a Vista or XP machine.
     
    I attempt to map to the test machine's C$ share and record the results in a text file.
    I then read the file and pull out the error code.
     
    Here is the function:
     
    Function View(strComputer)
    On Error Resume Next
    Const OpenAsDefault = -2
        Const FailIfNotExist =  0
        Const ForReading =  1
        Set oShell = CreateObject("WScript.Shell")
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
        sTempFile = sTemp & "\runresult.tmp"
        oShell.Run "%comspec% /c net use Z: \\" & strComputer & "\C$ " & ">" & sTempFile & " 2>&1", 0 , True
      
       Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
                                           FailIfNotExist, OpenAsDefault)
         sResults = fFile.ReadAll
         fFile.Close
    oShell.Run "%comspec% /c net use Z: /Delete >" & sTempFile & " 2>&1", 0 , True   
       
    oFSO.DeleteFile(sTempFile)

         If CBool(InStr(sResults, "successfully."))then    
          statval.value = strComputer & " C$ Is Available"
         ElseIf CBool(InStr(sResults, "2")) Then
          statval.value = strComputer & " File not found"
         ElseIf CBool(InStr(sResults, "3")) Then
          statval.value = strComputer & " Path not found."
         ElseIf CBool(InStr(sResults, "4")) Then
          statval.value = strComputer & " Too many files open."
         ElseIf CBool(InStr(sResults, "5")) Then
          statval.value = strComputer & " Access denied."
         Else
       statval.value = statval.value & VbCrLf & strComputer & " C$ Is Not Shared"
         End If
    '   DOS 2=File not found.
    '   DOS 3=Path not found.
    '   DOS 4=Too many files open.
    '   DOS 5=Access denied. 
     
    End Function
     
    The 2>&1  puts the standard error in the standard out pipe so everything is in one file.
    Oh and the function is part of an HTA which is why it writes out to statval.value.
    You might recognize the construction from Alex K. Angelopoulos great ping function.
     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://olddogsblog.spaces.live.com/blog/cns!C2DB05EEFA6C21A1!238.trak
    Weblogs that reference this entry
    • None