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.
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s