get-ServerUptime.ps1

Param (
  [string]$outputfilepath = "C:\TEMP\RedGreenUpTime.html", 
  [array]$servers = @("HOST1","HOST2","ETC") 
) 
 
Function Get-UpTime {
	Param ([string[]]$servers) 
	Foreach ($s in $servers) {  
  	If (Test-Connection -cn $s -Quiet -BufferSize 16 -Count 1) { 
    	$os = Get-WmiObject -class win32_OperatingSystem -cn $s  
      New-Object psobject -Property @{computer=$s; 
      uptime = (get-date) - $os.converttodatetime($os.lastbootuptime)}
		}
    Else {
			New-Object psobject -Property @{computer=$s; uptime = "DOWN"}
		}
  }
}
 
$style = @
  
 BODY{background-color:AntiqueWhite;} 
 TABLE{border-width: 1px;border-style: solid;border-color: Black;border-collapse: collapse;} 
 TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:DarkSalmon} 
"@ 
 
$precontent = @
 <h1>Server Uptime Report</h1> 
 <h2>The following report was run on $(get-date)</h2> 
"@ 
 
$i = 0 
 
ForEach($server in $servers) {
	$uptime = Get-UpTime -servers $server  
  $rtn = New-Variable -Name "$server$i" -PassThru 
  If ($uptime.uptime -eq "DOWN") {
		$uptime.uptime -eq "DOWN" 
    $upstyleRed = $style + "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Red} " 
    $rtn.value = $uptime | ConvertTo-Html -As Table -Fragment  -PreContent $upstyleRed | Out-String 
  }  
  Else {
		$uptime.uptime -eq "DOWN" 
    $upstyleGreen = $style + "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Green} </style>" 
    $rtn.value = $uptime | ConvertTo-Html -As Table -Fragment  -PreContent $upstyleGreen | Out-String 
  }  
 
	[array]$frags+=$rtn.Name 
	$i++ 
} 
 
$fragments = foreach($f in $frags) { Get-Variable $f | select -ExpandProperty value } 
ConvertTo-Html -PreContent $precontent -PostContent $fragments >> $outputfilepath  
#Invoke-Item $outputfilepath