automationScript

How to ping a network using VBScript

You can ping a network using VBScript. Yes, you read it right. We don’t know how many times we must have the necessity to ping a given network but, in order to do thta, you have to open command prompt and do that.

VBScript works so well with the windows system that you can do almost anything. Only limitation is your knowledge. Here we will see how to ping a network using VBScript.

VBScript Function to ping a network

Function Ping_Network(sHost)
	
'This function checks that you can get through to the host you require
'Example usage:
' 'Check we have internet...
' If Not Ping_Network("www.google.com") Then
' Msgbox "Not connected to Internet",vbCritical,"Fatal Error:"
' Call ExitTest()
' End If
	
	msgbox "Gen_Ping : " & sHost
	
	Dim oPing, oRetStatus
	
	Set oPing = GetObject("winmgmts:").ExecQuery ("select * from Win32_PingStatus where address = '" & sHost & "'")
	
	For Each oRetStatus In oPing
		
		If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
			
			msgbox "Gen_Ping Failed - Status code :" & oRetStatus.StatusCode
			
			Ping_Network= False
			
		Else
			
			msgbox "Gen_Ping OK - Bytes : " & vbTab & oRetStatus.BufferSize
			msgbox "Gen_Ping OK - Time(ms) : " & vbTab & oRetStatus.ResponseTime
			msgbox "Gen_Ping OK - TTL(s) : " & vbTab & oRetStatus.ResponseTimeToLive
			
			Ping_Network= True
			
		End If
		
	Next
	
	Set oPing = Nothing
	
End Function
Subscribe for updates