Get uptime for Servers( help)

This topic contains 1 reply, has 2 voices, and was last updated by  Simon Wåhlin 2 years, 6 months ago.

  • Author
    Posts
  • #25862

    Farrukh Sumair
    Participant

    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

    —————————————————————————————

  • #25863

    Simon Wåhlin
    Participant

    Move:

    $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:
    https://www.penflip.com/powershellorg/creating-html-reports-in-powershell

You must be logged in to reply to this topic.