Just fill the host file with host names you want to ping.

Script :

$PingMachines = Gc "C:\folder\hosts.txt"
ForEach($MachineName In $PingMachines){
	$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$MachineName'" |
	Select-Object StatusCode
	If ($PingStatus.StatusCode -eq 0){
		Write-Host $MachineName " : online" -Fore "Green"
	}
	Else{
		Write-Host $MachineName " : offline"  -Fore "Red"
	}
}

Reference
Win32_PingStatus class
The Win32_PingStatus  WMI class represents the values returned by the standard ping command. More information about ping can be found in RFC 791.
Starting with Windows Vista, Win32_PingStatus can return data for computers that have both IPv4 addresses and IPv6 addresses.
Windows Server 2003 and Windows XP: Win32_PingStatus only returns data for computers running IPv4. For more information, see IPv6 and IPv4 Support in WMI.
The following syntax is simplified from Managed Object Format (MOF) code and includes all of the inherited properties. Properties are listed in alphabetic order, not MOF order.

Syntax

class Win32_PingStatus 
{ 
  string  Address; 
  uint32  BufferSize = 32; 
  boolean NoFragmentation = FALSE; 
  uint32  PrimaryAddressResolutionStatus; 
  string  ProtocolAddress = ""; 
  string  ProtocolAddressResolved = ""; 
  uint32  RecordRoute = 0; 
  boolean ReplyInconsistency; 
  uint32  ReplySize; 
  boolean ResolveAddressNames = FALSE; 
  uint32  ResponseTime; 
  uint32  ResponseTimeToLive; 
  string  RouteRecord[]; 
  string  RouteRecordResolved[]; 
  String  SourceRoute = ""; 
  uint32  SourceRouteType = 0; 
  uint32  StatusCode; 
  uint32  Timeout = 1000; 
  uint32  TimeStampRecord[]; 
  string  TimeStampRecordAddress[]; 
  string  TimeStampRecordAddressResolved[]; 
  uint32  TimeStampRoute = 0; 
  uint32  TimeToLive = 80; 
  uint32  TypeofService = 0; 
};

The Win32_PingStatus class has these properties.
Address
Data type: string
Access type: Read-only
Qualifiers: Key
Value of the address requested. The form of the value can be either the computer name (“wxyz1234”), IPv4 address (“192.168.177.124”), or IPv6 address (“2010:836B:4179::836B:4179”).
BufferSize
Data type: uint32
Access type: Read-only
Qualifiers: Key
Buffer size sent with the ping command. The default value is 32.
NoFragmentation
Data type: boolean
Access type: Read-only
Qualifiers: Key
If TRUE, “Do not Fragment” is marked on the packets sent. The default is FALSE, not fragmented.
PrimaryAddressResolutionStatus
Data type: uint32
Access type: Read-only
Status of the address resolution process. If successful, the value is 0 (zero). Any other value indicates an unsuccessful address resolution.
ProtocolAddress
Data type: string
Access type: Read-only
Qualifiers: MaxLen (4096)
Address that the destination used to reply. The default is “”.
ProtocolAddressResolved
Data type: string
Access type: Read-only
Qualifiers: MaxLen (4096)
Resolved address corresponding to the ProtocolAddress property. The default is “”.
RecordRoute
Data type: uint32
Access type: Read-only
Qualifiers: Key
How many hops should be recorded while the packet is in route. The default is 0 (zero).
ReplyInconsistency
Data type: boolean
Access type: Read-only
Inconsistent reply data is reported.
ReplySize
Data type: uint32
Access type: Read-only
Represents the size of the buffer returned.
ResolveAddressNames
Data type: boolean
Access type: Read-only
Qualifiers: Key
Command resolves address names of output address values. The default is FALSE, which indicates no resolution.
ResponseTime
Data type: uint32
Access type: Read-only
Time elapsed to handle the request.
ResponseTimeToLive
Data type: uint32
Access type: Read-only
Time to live from the moment the request is received.
RouteRecord
Data type: string array
Access type: Read-only
Record of intermediate hops.
RouteRecordResolved
Data type: string array
Access type: Read-only
Resolved address that corresponds to the RouteRecord value.
SourceRoute
Data type: String
Access type: Read-only
Qualifiers: Key
Comma-separated list of valid Source Routes. The default is “”.
SourceRouteType
Data type: uint32
Access type: Read-only
Qualifiers: Key
Type of source route option to be used on the host list specified in the SourceRoute property. If a value outside of the ValueMap is specified, then 0 (zero) is assumed. The default is 0 (zero).

Value Meaning
0
None
1
Loose Source Routing
2
Strict Source Routing

 

 

StatusCode
Data type: uint32
Access type: Read-only
Ping command status codes.

0
Success

11001
Buffer Too Small

11002
Destination Net Unreachable

11003
Destination Host Unreachable

11004
Destination Protocol Unreachable

11005
Destination Port Unreachable

11006
No Resources

11007
Bad Option

11008
Hardware Error

11009
Packet Too Big

11010
Request Timed Out

11011
Bad Request

11012
Bad Route

11013
TimeToLive Expired Transit

11014
TimeToLive Expired Reassembly

11015
Parameter Problem

11016
Source Quench

11017
Option Too Big

11018
Bad Destination

11032
Negotiating IPSEC

11050
General Failure

Timeout
Data type: uint32
Access type: Read-only
Qualifiers: Key
Time-out value in milliseconds. If a response is not received in this time, no response is assumed. The default is 1000 milliseconds.
TimeStampRecord
Data type: uint32 array
Access type: Read-only
Record of time stamps for intermediate hops.
TimeStampRecordAddress
Data type: string array
Access type: Read-only
Intermediate hop that corresponds to the TimeStampRecord value.
TimeStampRecordAddressResolved
Data type: string array
Access type: Read-only
Resolved address that corresponds to the TimeStampRecordAddress value.
TimeStampRoute
Data type: uint32
Access type: Read-only
Qualifiers: Key
How many hops should be recorded with time stamp information while the packet is in route. A time stamp is the number of milliseconds that have passed since midnight Universal Time (UT). If the time is not available in milliseconds or cannot be provided with respect to midnight UT, then any time may be inserted as a time stamp, provided the high order bit of the Timestamp property is set to 1 (one) to indicate the use of a nonstandard value. The default is 0 (zero).
TimeToLive
Data type: uint32
Access type: Read-only
Qualifiers: Key
Life span of the ping packet in seconds. The value is treated as an upper limit. All routers must decrement this value by 1 (one). When this value becomes 0 (zero), the packet is dropped by the router. The default value is 80 seconds. The hops between routers rarely take this amount of time.
TypeofService
Data type: uint32
Access type: Read-only
Qualifiers: Key
Type of service that is used. The default value is 0 (zero).

Value Meaning

0
Normal

2
Minimize Monetary Cost

4
Maximize Reliability

8
Maximize Throughput

16
Minimize Delay
Ping computers in a host file

Leave a Reply

Your email address will not be published.