Archive for October, 2012

Log Ping Time and TTL using PowerShell

1. In PowerShell, enable remote signed
Set-ExecutionPolicy RemoteSigned
2. Create following powershell script to log down the result
$ping = new-object System.Net.NetworkInformation.Ping
$WriteLog = "ServerMonitorLog.csv"
$servers = "www.google.com","www.carters.com","hk.yahoo.com"
$FileExists = (Test-Path $WriteLog -PathType Leaf)

#If No Log File Exists, generate header
if(!($FileExists))
{
	Add-Content $WriteLog "Date,Time,Server,IPAddress,Time,TTL"
}

Foreach($s in $servers)
{
	$reply = $ping.send($s)
	$now = Get-Date
	
	if ($reply.status -eq "Success"){
		$result=[string]::Format("{0},{1},{2},{3},{4},{5}",$now.ToShortDateString(),$now.ToShortTimeString(),$s, $reply.Address.ToString(),$reply.RoundtripTime,$reply.Options.Ttl)
	}else{
		$z = [system.net.dns]::gethostaddresses($hostname)[0].ipaddresstostring
		$result=[string]::Format("FAIL,{0},{1}",$z,"***")
	}
	Add-Content $WriteLog $result
}

No Comments