Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Use PowerShell to Create an HTML Uptime Report

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and display an HTML server uptime report.

Hey, Scripting Guy! Question Hey, Scripting Guy! I have a rather unusual request. I need to create a server uptime report at work. But my boss wants me to create the report in HTML so the page can display on one of our intranet servers. I guess the idea is that the boss can go to this web page and see the current uptime of the servers when he wants to do so. I think this is more of a web developer kind of task (I do not know anything about HTML), but my boss is not the sort of person you say “No” to—people never seem to get a chance to say “No” to him twice anyway. Can you help me? I have seen all sorts of VBScript types of articles that seem to do what I want, but my boss also said we are moving away from VBScript, and we are now doing all new projects in Windows PowerShell—so help! By the way, if you are ever in Chicago, look me up and I will buy you and the Scripting Wife a pizza (you have saved me so many times, and I feel you will come through once again).

—RE

Hey, Scripting Guy! Answer Hello RE,

Microsoft Scripting Guy, Ed Wilson, is here. It is a little more than a month away, but the Charlotte PowerShell Saturday registration site is registering people at a really good clip. If you want to attend this all-star event, you should make your registration before it is too late. With three different tracks and a star-studded slate of speakers, this is going to be a major Windows PowerShell event. Due to technical considerations, the sessions will not be broadcast via Live Meeting (or Lync), so you will need to be in Charlotte on September 15, 2012 to see the sessions.

RE, a free pizza? Dude, how can I refuse—even better, Chicago pizza? Oh dude, I am all ears (just as Bugs Bunny once said to Elmer Fudd).

First obtain the server up time

The first thing that needs to be done is to obtain the uptime of the server. There are all sorts of things involved in this. (I even wrote a pretty involved article about this very subject for TechNet magazine: Calculating Server Uptime.) For the sake of argument, and for simplicity, I am going to use the LastBootupTime property from the Win32_OperatingSystem WMI class. When querying the value of that property, the number returned is not very easy to understand. This is shown here.

PS C:\> (gwmi win32_operatingsystem).lastbootuptime

20120806121018.496471-240

Luckily, WMI objects in Windows PowerShell have a method called ConvertToDateTime that knows how to convert the WMI time to a normal DateTime object. Therefore, all that needs to occur is to store the returned WMI object in a variable, and call the method. This technique is shown here.

PS C:\> $wmi = gwmi win32_operatingsystem

 

PS C:\> $wmi.ConvertToDateTime($wmi.LastBootUpTime)

 

Monday, August 6, 2012 12:10:18 PM

Because I am going to use this information later, I want to create a custom object that returns only the computer name and the uptime. I could use the Select-Object cmdlet to do this, but today I feel like I will use the New-Object cmdlet and create a new psobject. This code is shown here.

     $os = Get-WmiObject -class win32_OperatingSystem -cn $s

     New-Object psobject -Property @{computer=$s;

       uptime = (get-date) - $os.converttodatetime($os.lastbootuptime)}

I put all of this information into a function, and I configure it to accept an array of strings for the input. Here is the Get-UpTime function.

Function Get-UpTime

{ Param ([string[]]$servers)

  Foreach ($s in $servers)

   {

     $os = Get-WmiObject -class win32_OperatingSystem -cn $s

     New-Object psobject -Property @{computer=$s;

       uptime = (get-date) - $os.converttodatetime($os.lastbootuptime)}}}

Creating an HTML report from within PowerShell

The entry point to the script contains two variables. These variables are exposed as command-line parameters to the script. You can modify the default values, and simply run the script as you wish. Here are the command-line parameters.

Param(

  [string]$path = "c:\fso\uptime.html",

  [array]$servers = @("dc1","dc3","ex1","hyperv1","hyperv2","hyperv3")

)

The part that creates the HTML report is really simple. It uses the ConvertTo-HTML cmdlet. Here is the cool part of the script. I call the Get-UpTime function and pass the servers from the command-line parameter, $servers. I pipe the custom PSObject that returns from the Get-Uptime function to the ConvertTo-HTML cmdlet, and I create a table. The only HTML code I use is to tell the Server Uptime Report to display as an H1 heading. It is larger and bolder than using the –title parameter. Also, by placing it in the body of the cmdlets, I can control the way everything displays a bit better. I use a subexpression to return the current date and time, and I redirect the HTML output to file specified by the $path parameter. If the file does not exist, it is created. To display it, I use the Invoke-Item cmdlet and pass the path. Here is the main portion of the script.

Get-UpTime -servers $servers |

ConvertTo-Html -As Table -body "

  <h1>Server Uptime Report</h1>

  The following report was run on $(get-date)" >> $path

 Invoke-Item $path 

I upload the complete HTML_UptimeReport.ps1 script to the Scripting Guys Script Repository. You can obtain the script from this location.

When the script runs, it displays the output shown here.

Image of command output

RE, that is all there is to using Windows PowerShell to create an HTML uptime report. Join me tomorrow for more Windows PowerShell cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>