Hi
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 *
$Count=0
$Success = 0
$Unreachable = 0
$Failed = 0
$FinalOutput = @()
#$serverlist = $ServerData.name
$serverlist = ‘Server1’,‘Server2’,‘Server3’,‘Server4’
foreach($Server in $Serverlist) {
$Count++
#$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
$Status=1
$Success++
} catch {
#$OutputObj.Uptime = ‘Unable to get’
$OutputObj | Add-Member -MemberType NoteProperty -Name Uptime -Value “Unable to get”
$Failed++
}
}
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 “”
$Unreachable++
}
}
$FinalOutput +=$OutputObj
$Report = "
Server Uptime Report
Server Uptime Report
Report Generated at $(Get-Date)
Server Name
Is Online
Uptime
"
if($upfor.days -lt ‘30’) {
$BGColor=“green”
} else {
$BGColor=“red”
}
$Report += "
$($OutputObj.‘Server Name’)
$($OutputObj.‘Is Online’)
$($OutputObj.Uptime)
"
$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