Link to home
Start Free TrialLog in
Avatar of MikeCriss
MikeCriss

asked on

VBScript to determine if a Computer is available on a network

I need a VBScript to determine if a specific computer is available on my network.

I have two W2000 Servers, one is the Primary Domain controller and the other is a secondary controller. They are used primarily for file servers. I have software installed that replicates files in real-time between the two servers. I need a script that I can add to my current script that will determine if primary server is available. If not my script will map the users drives to the backup server.
Avatar of MikeCriss
MikeCriss

ASKER

I should also mention that I need the script to work with W98, W2000, and XP.
Try the following script to determine whether the primary Domain controller is available or not....

I have added a check for Windows NT and 98. But I don't have any Win9X machine available with me.
If you find any issue on 98 then let me know about the same.

'Specify the IP Address of Domain controller or the host name.
'For example: use 'SERVER1' if you use '\\SERVER1' to access the server.
strPrimaryController = "10.20.30.40"
msgbox blnPing(strPrimaryController)
Function blnPing(strHostAddress)
  Dim strCommand, strLine
  On Error Resume Next
  blnPing = False
  'Issue ping command and output results to temp file
  Set objShell = WScript.CreateObject("WScript.Shell")
  blnTestNT = objShell.ExpandEnvironmentStrings("%OS%")
  If UCase(blnTestNT) = "WINDOWS_NT" Then
    strCommand = "cmd /c ping -n 3 -w 10000 " & strHostAddress
  Else
    strCommand = "command /c ping -n 3 -w 10000 " & strHostAddress
  End If
  Set objShellScriptExec =  objShell.exec(strCommand)
  Set objStdOut = objShellScriptExec.StdOut
  If err.Number <> 0 Then
    Call Msgbox ("Script is not able to determine the Status of entered server." & vbCrLf & _
    "Error: " & err.Description, vbCritical, "Error occured." )
    WScript.Quit 1
  End If
   'Process SID command results
   Do While NOT objStdOut.AtEndOfStream
      strLine = objStdOut.ReadLine
      If InStr(strLine, "Reply") > 0 AND _
           Not (InStr(UCase(strLine), "UNREACHABLE") > 0) Then
          blnPing = True
          Exit Do
      End If
   Loop
  On Error Goto 0
End Function
Are the users of the w98, w2000 and xp machines authenticating to the Windows 2000 environment? What are some assumptions we can work with to give you what you are looking for.

For example is the Win98 machines Active Directory aware?

Thanks!

Tone
TiwariVikas,
I have used the "cmd /c ping" for the Windows 2000 and XP machines but I did not know what command to use for the Windows 98 machines.

Thanks for the code. I will be out of the office all next week, but I will try it as soon as I get back and let you know how it works for me.

tone28,

My users are all authenticating on a Windows 2000 Domain. The Windows 98 machines have been updated with Active Directory extensions. I created a Logon.vbs file for the domain that checks for the OS and the users group, then maps the corrosponding network drives for that group. For the Windows 98 machines I used a batch file that calls the VBScript file at logon.
TiwariVikas,
I tried the code. It works for Windows 2000 and XP but always returns False for Windows 98.

Any Suggestions?
Mike,

I don't have any Win98 machine as of now... Can you please try the following command on command prompt and post the results so that I can modify the script based on that:

GoTo Start -> Run . Type "command". In the opened dos prompt try

1) ping localhost
2) ping FAKESERVER
3) ping ANYKNOWNSERVER

please let me know the results of all the cases ping command returns on Win98.

Regards,
Vikas.

1) ping localhost =

C:\>ping localhost

Pinging Laser.WD.local [127.0.0.1] with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128
Reply from 127.0.0.1: bytes=32 time<10ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms


2) ping FAKESERVER =
C:\>ping fakeserver
Unknown host fakeserver.

3) ping ANYKNOWNSERVER =

C:\>ping Fileserv

Pinging Fileserv.WD.local [10.1.1.225] with 32 bytes of data:

Reply from 10.1.1.225: bytes=32 time<10ms TTL=128
Reply from 10.1.1.225: bytes=32 time=1ms TTL=128
Reply from 10.1.1.225: bytes=32 time=1ms TTL=128
Reply from 10.1.1.225: bytes=32 time=1ms TTL=128

Ping statistics for 10.1.1.225:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  1ms, Average =  0ms


TiwariVikas,
I found this code on Microsoft's website. It works for Windows 2000, NT, XP, 98 and 95.
Is there a way to make the command window hidden?
 
strComputer = "client1"
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec( _
    "ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
    If InStr(strPingResults, "destination net unreachable") Then
        WScript.Echo strComputer & "did not respond to ping."
    Else
        WScript.Echo strComputer & " responded to ping."
    End If
Else
    WScript.Echo strComputer & " did not respond to ping."
End If
The script would have to be run using cscript
ASKER CERTIFIED SOLUTION
Avatar of TiwariVikas
TiwariVikas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TiwariVikas,

Thank you for all the help. The code works great for all of my Operating Systems. Exactly what I needed!

By the way, I found that you can use "%comspec%" instead of "cmd" or "command". It works for 98, NT, XP, and 2000, then you don't have to check for the Operating System. I don't have any 2003 systems, so I couldn't test it for 2003.

Thanks Again