• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2140
  • Last Modified:

VBScript to check if Server is hung

Hi Team,

I am looking for a VBScript that will run on multiple server and let me know it is hung.  Below is the scenario and requirement,

Scenario:

1)  I Servers that reboot overnight.
2)  The reboot is initiated and server reboots, but while booting up it is struck in Blue Screen.
3)  When you try to Ping the server, it pings (I get reply)
4)  But when you try to do RDP to server it failes.  
5)  We need to take the Remote control thrugh iLO or DRAC and reboot the server or inform the Datacentre team to reboot it manually.

Requirement:

I need a script which will run against multiple server and give me the following Result:
1)  Whether the Server is Pingable
2)  Whether the RDP to server is successfull or not (or any other variable that can tell me that server is hung).

Praveen
0
praveendusi
Asked:
praveendusi
  • 4
  • 3
1 Solution
 
Tony MassaCommented:
You don't necessarily have to use RDP, but here's a script that should get you what you need.

 
On Error Resume Next

ServerList = "ServerList.txt"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
x = 0
y = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(ServerList, ForReading)
Set objShell = CreateObject("Wscript.Shell")
Set objResults = objFSO.CreateTextFile("ServerCheckResults.txt", ForWriting)

Do Until objList.AtEndOfStream

machine = UCASE(objList.ReadLine)

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select * from Win32_PingStatus where address = '"_
            & machine & "'")
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
        	'objResults.Writeline UCASE(machine) & " ping failed...checking RDP"
            TestRDP()
        Else
        	'objResults.WriteLine machine & " ping successful"
        	TestRDP()
        End If
    Next
 Loop
 
Function TestRDP()
  strCommand = "portqry -n " & machine & " -e 3389"
  Set objExec = objShell.Exec(strCommand)
  strResult = objExec.StdOut.ReadAll()
  If Instr(strResult, "Failed to resolve") Then
  	objResults.WriteLine machine & ": Could not resolve IP"
  End If
  If Instr(strResult, "NOT LISTENING") Or Instr(strResult, "FILTERED") Then
    objResults.WriteLine machine & ": RDP port is not responding, check to see if server is hung"
  End If
End Function

Open in new window

0
 
Tony MassaCommented:
Whomever will be running this script will need the PORTQRY utility installed to a directory included in the PATH variable of their system: (e.g. C:\Windows\System32)

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=89811747-c74b-4638-a2d5-ac828bdc6983&displaylang=en
0
 
praveendusiAuthor Commented:
Hi Tmassa99,

Thank you for the script, script works good.  But need the output in CSV and status for each server rather than just showing the server which is not responding to port 3389.

Let me explain you another peculiar scenario which I have noiticed.

1)  Ping Works fine
2)  I am able to access the share drives and folders on the server
3)  I am able to start and stop the Services.

Although all the mentioned above work, RDP do not work, gets an error message "This Computer can't Connect to the remote computer"

When I take the iLO or DRAC session, I see that server is struck at Blue Blank Screen.  Only thing is I need reboot it.

Can we track this with VBScript?

Praveen

0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Tony MassaCommented:
On Error Resume Next

ServerList = "ServerList.txt"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
x = 0
y = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(ServerList, ForReading)
Set objShell = CreateObject("Wscript.Shell")
Set objResults = objFSO.CreateTextFile("ServerCheckResults.csv", ForWriting)

objResults.Writeline "ComputerName,PingStatus,RDP Status"

Do Until objList.AtEndOfStream

machine = UCASE(objList.ReadLine)

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select * from Win32_PingStatus where address = '"_
            & machine & "'")
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
        	'Ping Failed
        	x = 1
          TestRDP()
        Else
        	'Ping Success
        	x = 0
        	TestRDP()
        End If
    Next
 Loop
 
Function TestRDP()
  strCommand = "portqry -n " & machine & " -e 3389"
  Set objExec = objShell.Exec(strCommand)
  strResult = objExec.StdOut.ReadAll()
  If Instr(strResult, "Failed to resolve") Then
  	y = 9
  	Exit Function
  End If
  If Instr(strResult, "NOT LISTENING") Or Instr(strResult, "FILTERED") Then
    y = 1
    Exit Function
  End If
  y = 1
End Function

Function WriteOutput()
  If x = 0 and y = 0 Then
  	objResults.Writeline machine & ",success,success"
  Else If x = 0 and y = 1 Then
  	objResults.Writeline machine & ",success,fail"
  Else If x = 1 and y = 9 Then
  	objResults.writeline machine & ",Name Resolution Failed"
  Else If x = 1 and y = 1 Then
  	objResults.Writeline machine & ",failed,failed"
  End If
  End If
  End If
  End If
  x = 0
  y = 0
End Function

Open in new window

0
 
praveendusiAuthor Commented:
Hi Tmassa99,

This still gives me the result only for the server where port 3389 is failed not the status for all servers.

As I have epxplained my second scenario, can have a script which can initiate RDP connect for each server in txt file and report the status i.e. If connected "Success" and if failed (i.e. if we get error "cannot connect to remote computer" then "Failed"... Will that be possible?

If this is possible this will make my life easier.

Praveen
0
 
Tony MassaCommented:
This gives you status for all servers checked.

You get 3 colums
SERVERNAME        PINGSTATUS     RDPSTATUS

Every host in the server list will report
0
 
Robin CMSenior Security and Infrastructure EngineerCommented:
Two suggestions:
Find out why the servers are bluescreening and fix it.
Set the server to automatically reboot after getting a bluescreen. Either do this by ticking the box from My Computer - Properties, Advanced, Startup and Recovery - Settings, System Failure - Automatically Restart. Or by using the instructions with your lights out controller (e.g. some Dell DRACs can record an image of the bluescreen before restarting the server). Though presumably if you had DRAC or iLO controllers you'd just be rebooting them yourself...
0
 
praveendusiAuthor Commented:
Thank you for the solution
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now