VBScript to Check if Machine is Online or Offline

In this article, I am going write vbscript sample to check if a given computer is online (up) or offline (down) and vbscript to check ping status of set of hostnames (from text file-txt) and export its output to CSV file.

Vbscript to find if a machine is Online or not

1. Copy the below example vbscript code and paste it in notepad or a vbscript editor.
2. Save the file with a .vbs extension, for example: PingStatus.vbs
3. Change the hostname with your computer name which you want to check ping status.
4. Double-click the vbscript file (or Run this file from command window) to check machine is active or not.

Dim hostname
hostname = "Your-PC"
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & hostname, 0, True)
Select Case Ping
Case 0 
   WScript.Echo "The machine '" & hostname & "' is Online"
Case 1 
   WScript.Echo "The machine '" & hostname & "' is Offline"
End Select

Export Ping status of set of Computers to CSV file

Use the below vbscript code to check ping status for multiple machines and get output in csv file. First create the text file HostNames.txt which includes one computer name in each line.

Dim hostTxtFile, outCSVFile,csvColumns,hostName
Const ForReading = 1

' Create CSV file 
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
outCSVFile ="C:\HostStatus.csv"
Set objCSVFile = objFSO.CreateTextFile(outCSVFile, _ 
    ForWriting, True)
' Write comma delimited list of columns in CSV output file.
csvColumns = "Hostname,Status"
objCSVFile.Write csvColumns
objCSVFile.Writeline

' Give the path of text file which contains set of hostnames (one hostname per line).
hostTxtFile = "C:\HostNames.txt"
' Open the file for reading.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(hostTxtFile, ForReading)

' Read the computer names from text file and check ping status.
Do Until objFile.AtEndOfStream
' Read computer name
hostName = objFile.ReadLine
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & hostName, 0, True)
Select Case Ping
Case 0 
        objCSVFile.Write hostName & "," 
        objCSVFile.Write "Online" & ""
Case 1 
        objCSVFile.Write hostName & "," 
        objCSVFile.Write "Offline" & ""
End Select
objCSVFile.Writeline

Loop

Note: If you open the csv file using MS Excel, the line should comes in a single cell, to fix this issue, copy the content of the csv file and paste in new text file and save it as new .csv file.

Advertisement

2 thoughts on “VBScript to Check if Machine is Online or Offline”

  1. To check your own PC:

    ̷h̷o̷s̷t̷n̷a̷m̷e̷ ̷=̷ ̷”̷Y̷o̷u̷r̷-̷P̷C̷”̷ ————–> hostname = “google.com”

    so it works !

    Reply

Leave a Comment