Get uptime for Servers( help)


I have a script which I am working on… It get the uptime of few server and displays in html format. once I run this one, I cannot get uptime for all server but just one. I can understand that I am creating new object in for each loop. I cannot figure out how to use it for more than one servers…

Any help??

Import-Module ActiveDirectory
[array]$ServerData = Get-ADComputer -Filter { OperatingSystem -like ‘Server’} -properties *
$Success = 0
$Unreachable = 0
$Failed = 0
$FinalOutput = @()
#$serverlist = $
$serverlist = ‘Server1’,‘Server2’,‘Server3’,‘Server4’

foreach($Server in $Serverlist) {

     #$OutputObj = "" | Select 'Server Name','Is Online','Uptime'
     $OutputObj = New-Object -TypeName PSobject
     #$OutputObj.'Server Name' = $server
     $OutputObj | Add-Member -MemberType NoteProperty -Name 'Server Name' -Value $server

$Status = 0

     if(Test-Connection -Computer $Server -count 1 -ErrorAction SilentlyContinue) {
         #$OutputObj.'Is Online' = 'True'
         $OutputObj | Add-Member -MemberType NoteProperty -Name 'Is Online' -Value "True"

try {
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $server -ErrorAction stop
$LastBootUpTime = $os.ConvertToDateTime($os.LastBootUpTime)
$LocalDateTime = $os.ConvertToDateTime($os.LocalDateTime)
$upfor = $LocalDateTime - $LastBootUpTime
$uptime = “$($upfor.Days) days”
#$OutputObj.Uptime = $uptime
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value $Uptime
} catch {
#$OutputObj.Uptime = ‘Unable to get’
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value “Unable to get”
else {
#$OutputObj.‘Is Online’ = ‘FALSE’
#$OutputObj.Uptime = “”
$OutputObj | Add-Member -MemberType NoteProperty -Name ‘Is Online’ -Value “False”
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value “”
$FinalOutput +=$OutputObj

$Report = "

       Server Uptime Report 

Server Uptime Report

    Report Generated at $(Get-Date)

Server Name
Is Online


if($upfor.days -lt ‘30’) {
} else {
$Report += "
$($OutputObj.‘Server Name’)
$($OutputObj.‘Is Online’)

         $Report +="

Report Summary:

Total Servers
: $Count

   Total Servers Online
   : $Success
   Total Servers Offline
   : $Unreachable
   Total Servers Failed to Query
   : $FailedComps


$Report | Out-File .\UptimeReport.html -Force


$FinalOutput += $OutputObj

To within the loop.

Then you need to add $FinalOutput to $Report, I suggest using ConvertTo-Html like this:

$Report += $FinalOutput | ConvertTo-Html -Fragment

For more info on how to create HTML report, there is a free ebook on Creating HTML Reports in PowerShell: