Generate a server uptime HTML report with PowerShell

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

Sitaram Pamarthi By Sitaram Pamarthi - Fri, September 21, 2012 - 12 comments

Sitaram Pamarthi is working as a Windows Engineer and his special fields of interest are PowerShell, Active Directory, Exchange, and virtualization.

Contents of this article
  1. Usage and help
  2. Parameters
  3. Remarks

“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:

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.

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:

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 ————————–

————————– EXAMPLE 2 ————————–

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

————————– EXAMPLE 3 ————————–

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

-1+1 - Rate this post
Loading ... Loading ...
Your question wasn't answered? Ask in the new 4sysops forum!

12 Comments- Leave a Reply

  1. Tejas Kshirsagar says:

    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. techibee.com says:

    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. Gary says:

    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. techibee.com says:

    @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. Tejas Kshirsagar says:

    @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. Tejas Kshirsagar says:

    @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. techibee.com says:

    @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. Tejas Kshirsagar says:

    Oh Yes, that’s correct.

  9. Gary says:

    Renaming the script resolved the issue. Thanks.

  10. Joe says:

    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. suresh says:

    need correct path to dowload the script

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

===Leave a Comment===

Login

Lost your password?