?
Solved

VBScript to determine if a Computer is available on a network

Posted on 2006-06-16
11
Medium Priority
?
715 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:MikeCriss
  • 6
  • 3
  • 2
11 Comments
 

Author Comment

by:MikeCriss
ID: 16923421
I should also mention that I need the script to work with W98, W2000, and XP.
0
 
LVL 2

Expert Comment

by:TiwariVikas
ID: 16925842
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
0
 
LVL 6

Expert Comment

by:tone28
ID: 16929653
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MikeCriss
ID: 16930297
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.
0
 

Author Comment

by:MikeCriss
ID: 16985224
TiwariVikas,
I tried the code. It works for Windows 2000 and XP but always returns False for Windows 98.

Any Suggestions?
0
 
LVL 2

Expert Comment

by:TiwariVikas
ID: 16985453
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.
0
 

Author Comment

by:MikeCriss
ID: 16986713

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


0
 

Author Comment

by:MikeCriss
ID: 16987386
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
0
 
LVL 6

Expert Comment

by:tone28
ID: 16987534
The script would have to be run using cscript
0
 
LVL 2

Accepted Solution

by:
TiwariVikas earned 1000 total points
ID: 16991248
Hi,

ObjShell.Exec() method doesn't have options to hide the command window.

If you want to hide it then use objShell.Run(strCommand, 0 , True)
and redirect the output to a text file. then reading that text file u can get the desired results.

Try this out and if it is still not functioning for Win98 then let me know the values of the dialogs coming up from script :

'Specify the IP Address of Domain controller or the host name.
'For example: use 'SERVER1' if you use '\\SERVER1' to access the server.
strHostAddress = "localhost"
Dim strCommand, strLine
Const ForReading = 1
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%")
msgbox "blnTestNT: " & blnTestNT
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
strTempPath = objShell.ExpandEnvironmentStrings("%temp%")
Set fso = CreateObject("Scripting.FileSystemObject")

strTempFile = strTempPath & "\test.log"
Call objShell.Run( strCommand & " > " & strTempFile,0,True)

Set objScriptExec = fso.OpenTextFile(strTempFile, ForReading)

strPingResults = LCase(objScriptExec.ReadAll)
msgbox strPingResults
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
On Error Goto 0
0
 

Author Comment

by:MikeCriss
ID: 16992152
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question