The PowerShell script discussed in this post will ping a list of servers and get their uptime in HTML report format.
Sitaram Pamarthi

"Send me the server uptime report” is what we system administrators often hear from our bosses. Of all reporting formats, I feel the HTML report is the best way to report this status. Keeping this requirement in mind, I came up with a PowerShell script that takes a list of servers as input and generates an uptime report with a report summary in HTML format.

Server Uptime Report with PowerShell

Server Uptime Report with PowerShell

Though the script looks quite long, it is a wrapper around the Win32_OperatingSystem WMI class. We are generating a list of server uptimes by querying the LastBootUpTime property of this WMI class. This property contains the uptime in WMI time format, which is not that easy to understand. So, I am using the ToDateTime method of the [System.Management.ManagementDateTimeconverter] class to convert the WMI time to the local system date time format. This is a code sample:

$Boottime = (Get-WmiObject win32_operatingSystem -computer $Computer -ErrorAction stop).lastbootuptime
$Boottime = [System.Management.ManagementDateTimeconverter]::ToDateTime($BootTIme)

The value returned after the time conversion is the timestamp when the server last rebooted. But this is not the uptime. To calculate the uptime, we simply need to subtract the last boot time from the current time (it makes sense, doesn't it?).

Though the date subtraction looks quite complex, it is not that tough in the PowerShell world. A cmdlet, New-TimeSpan, is available in PowerShell to calculate the time span between two dates. The output gives the difference between two timestamp values in terms of number of days, hours, minutes, and seconds. See the code below to understand how the time difference calculation works.

$Now = Get-Date
$span = New-TimeSpan $BootTime $Now
$Uptime = "{0} day(s), {1} hour(s), {2} min(s), {3} second(s)" -f $span.days, $span.hours, $span.minutes, $span.seconds

I used some HTML code in the begin{} and end{} blocks of the script to form the tables and summary report. Some HTML code in the process{} block will be generated based on the status of the servers it is processing.

This script has an -HTMLReport parameter that, when specified, causes the script to generate the output in HTML format and save it to c:\ with the file name UptimeReport.HTML. You can change the location of the output report by specifying the -HTMLFile parameter. When the -HTMLReport parameter is not specified, the script will output the details to the console in object format, which you can pass to other scripts if you want or format it the way you need.

Usage and help

You can get usage instructions and help about this script by using the following command:

PS C:\scripts> get-help .\Get-ServersUptime.ps1 -Detailed
NAME
C:\scripts\Get-ServersUptime.ps1
SYNOPSIS
Query uptime details of servers.
SYNTAX
C:\scripts\Get-ServersUptime.ps1 [[-ComputerName] <String[]>] [-HTMLReport] [[-HTMLFile] <String>] [<CommonParameters>]

This script helps you get the uptime details of the servers. It also generates an HTML report when the -HTMLReport switch is used. The report contains the uptime details and a summary of how many computers are reachable and how many are not.

Parameters

-ComputerName <String[]>

Computer name(s) for which you want to get the uptime details.

-HTMLReport [<SwitchParameter>]

Generates an HTML report in c:\ drive with name uptimereport.html by default. You can override this by specifying the HTML file parameter.

-HTMLFile <String>

Name of the file path where you want to store the report.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer and OutVariable.

For more information, type:

"get-help about_commonparameters".

-------------------------- EXAMPLE 1 --------------------------

C:\PS>Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2
Gets the uptime of Comp1 and Comp2.

-------------------------- EXAMPLE 2 --------------------------

C:\PS>Get-UptimeOfServers.ps1 -ComputerName Comp1, Comp2 -HTMLReport

Gets the uptime of Comp1 and Comp2 and saves the report in HTML format.

-------------------------- EXAMPLE 3 --------------------------

C:\PS>Get-Content c:\servers.txt | Get-UptimeOfServers.ps1 -HTMLReport

Gets the uptime of computers listed in servers.txt and saves the report in HTML format.

Remarks

To see the examples, type: "get-help C:\scripts\Get-ServersUptime.ps1 -examples".

For more information, type: "get-help C:\scripts\Get-ServersUptime.ps1 -detailed".

For technical information, type: "get-help C:\scripts\Get-ServersUptime.ps1 -full".

Download the PowerShell script: Get-ServerUpTime.ps1

15 Comments
  1. Avatar
    Tejas Kshirsagar 12 years ago

    On running script.I observed that it doesnot show up correct time.

    For Windows 2003 servers – output gives 10 seconds less time.
    For Windows 2000 servers – output gives 2 hrs 14 seconds less time.
    For Windows 2008 R2 servers – output gives 24 seconds more time.

    Can you please check & suggest modifications to perform in script.

  2. Avatar Author
    techibee.com 12 years ago

    That is strange. I didn’t notice any such problems. A few questions to help you further:

    1. Are these servers are in different timezone when compared with the system where you are running the script?
    2. Could you please confirm that you cross verified the output with traditional uptime.exe?

  3. Avatar
    Gary 12 years ago

    When I run the script .\get-uptimeofservers.ps1 -computername USTEST001 – I’m receiving the below error:

    The term ‘.\get-uptimeofservers.ps1’ is not recognized as a cmdlet, function, operable program, or script file. Verify
    the term and try again.
    At line:1 char:26
    + .\get-uptimeofservers.ps1 <<<< -computername USTEST001

  4. Avatar Author
    techibee.com 12 years ago

    @Gary,
    I think you are not running the script from the directory where you stored it. You need to CD to that folder and execute it from there.

    Ex: If the script is in “C:\temp” folder
    cd c:\temp
    .\get-uptimeofservers.ps1

    Alternatively, you can also use the absolute path of the script to execute it.
    Ex:
    c:\temp\get-uptimeofservers.ps1

    Hope this helps.

  5. Avatar
    Tejas Kshirsagar 12 years ago

    @Gary,

    Please rename the PS1 script name to something else like “Uptime.ps1”. Seems the PS1 name & name you are running for execution are not correct as like above for downloading ps1 file has space in name.

    Hope this helps to you.

  6. Avatar
    Tejas Kshirsagar 12 years ago

    @Gary,

    Please rename the PS1 script name to something else like “Uptime.ps1”. Seems the PS1 name & name you are running for execution are not correct as like above for downloading ps1 file has space in name.

    Hope this helps to you.

  7. Avatar Author
    techibee.com 12 years ago

    @Tejas, Thanks for pointing the space problem in the download link. I will get it corrected. Btw, that shouldn’t cause the problem here. When you click on the link and save as that it is coming properly. Moreover, if you have space in the script name, you will get different error message. See below. Notice the space after “-“.

    PS C:\Users\localuser> .\get- myhosts.ps1
    The term ‘.\get-‘ is not recognized as the name of a cmdlet, function, script f
    ile, or operable program. Check the spelling of the name, or if a path was incl
    uded, verify that the path is correct and try again.
    At line:1 char:7
    + .\get- <<<

  8. Avatar
    Tejas Kshirsagar 12 years ago

    Oh Yes, that’s correct.

  9. Avatar
    Gary 12 years ago

    Renaming the script resolved the issue. Thanks.

  10. Avatar
    Joe 12 years ago

    Love the script, however it doesn’t look like the counting feature works when reading from a servers.txt file. In the report it displays each S.No as 1. Also, the report summary doesn’t have the correct numbers displayed.

    However, the following command displays the count correctly

    .\Get-UptimeOfServers.ps1 -ComputerName comp1, comp2 -HTMLReport

    Any idea of what the problem may be?
    Thanks in advance

  11. Avatar
    suresh 11 years ago

    need correct path to dowload the script

  12. Avatar
    Michael Pietroforte 11 years ago

    Suresh, the link is at the end of the text.

  13. Avatar
    Srinivas 9 years ago

    The script is fabulous but, I have tried executing the script but the server count stating as 1 for all multiple servers. Could you please assist.

  14. Avatar
    Matthew Quickenden 8 years ago

    Hi, I have created an HTML reporting module to help generate HTML code from within Powershell.  You can group data, color rows, create sections, roll up sections and load your own logos and more all without any HTML coding.   Follow this link for code and examples, it is also loaded in github for collaborative development.

    http://www.azurefieldnotes.com/2016/08/04/powershellhtmlreportingpart1/

     

  15. Avatar
    HB 7 years ago

    how do you sort the results by column?

Leave a reply

Please enclose code in pre tags: <pre></pre>

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2024

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending
WindowsUpdatePreventer

Log in with your credentials

or    

Forgot your details?

Create Account