Hey, Scripting Guy! How Can I Have Computer Configuration Information Written to an HTML File?

Hey, Scripting Guy! How Can I Have Computer Configuration Information Written to an HTML File?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to obtain some basic configuration information from my computers on the network. It would be nice if the script would write the output in an HTML file that we could copy to our internal Web server. This would give us easy access to the information. Whadda ya say?

- JE

SpacerHey, Scripting Guy! Answer

Hi JE,

This morning, it is really cold, gray, and yucky outside. The weather makes me feel as if I am in Seattle, Washington, in the United States, instead of Charlotte, North Carolina. With enough gray outside, there is no reason to invite gloomy Gus into our data center. It is with great joy, therefore, that I read your e-mail message because by writing output to an HTML file, we can bring a little color into our otherwise dreadful monotone existence. This is the kind of script that you can make as long and as complicated—or as short and simple—as you wish. As I was writing the script, what started as short and simple became a bit longer and only slightly less simple as the morning wore on.

If interested, you can learn more about saving data as HTML. You can also use a VBScript approach to create Web pages. And this article talks about inventorying computer hardware.

Here is the SystemConfigToHtml.ps1 script:

$body = $null
$computer = "localhost"
$System = Get-WmiObject -Class win32_computersystem -ComputerName $computer
$processor = Get-WmiObject -Class win32_processor -ComputerName $computer
$video = Get-WmiObject -Class win32_videocontroller -ComputerName $computer
$disk = [wmi]"\\$computer\root\cimv2:win32_logicaldisk.deviceID='c:'"
$body += "Processor speed " + $processor.CurrentClockSpeed + "<br>"
$body += "Video ram (meg):  " + $video[0].AdapterRAM/1mb + "<br>"
$body += "Free disk space(gig): " + $disk.FreeSpace/1gb + "<br>"
$body += "Make: " + $system.Manufacturer + "<br>"
$body += "Model: " + $system.Model + "<br>"
$body += "Ram(meg): " + [int]$system.TotalPhysicalMemory/1mb
$head =  "<style>"
$head += "BODY{background-color:peachpuff}"
$head += "</style>"
$head += "<h1>System report for $computer </h1><br>"
ConvertTo-Html  -body $body -Head $head|
Out-File -FilePath "C:\fso\$computer.html"

The script begins by initializing a couple of variables. The first variable, $body, is used to hold the main text we want to display on the Web page. The $computer variable is used to hold the name of the computer to query. This is seen here:

$body = $null
$computer = "localhost"

Next, what we want to do is obtain some basic computer system information. This is the same WMI class you have probably used many times before in VBScript. To query WMI with Windows PowerShell, you use the Get-WmiObject cmdlet. In a script, I recommend using all the parameter names, such as –class and –computername, but they are not really required. This returns all the information from the WMI class and stores it in the $system variable.

$System = Get-WmiObject -Class win32_computersystem -ComputerName $computer

We then want to obtain some information about the processor. We use the win32_processor WMI class. We use the same style of command as the previous line and store the results in the $processor variable (this VBScript retrieves the same information):

$processor = Get-WmiObject -Class win32_processor -ComputerName $computer

We also want to gather some information about the video card. To do this, we use the Win32_videoController WMI class. We store the video controller information in the $video variable (here is a VBScript that retrieves the same information):

$video = Get-WmiObject -Class win32_videocontroller -ComputerName $computer

We decide to connect specifically to the C: drive on the computer. To do this, we use the [WMI] type accelerator. This allows us to provide the path to the specific drive we are interested in. This is similar to using the Get method from the SwbemServices object you may have used in VBScript:

$disk = [wmi]"\\$computer\root\cimv2:win32_logicaldisk.deviceID='c:'

Now we want to build up the information that will be printed out. Here we switch gears and combine Windows PowerShell with HTML. To add information to the $body variable, we use the += operator. This is the same thing as saying $body = $body + "whatever". The += operator is much easier to type. To include a new line in HTML, we use the <br> tab; we place it at the end of each line. For the processor, we were only interested in the CurrentClockSpeed property, so we choose that from the $processor variable. All the processor information is included in the variable, and we could use any of the information contained in the Win32_Processor class.

The last thing we need to see is the use of the administrative constants: 1mb and 1gb. We use these to convert the number from a whole lot of bytes to megabytes and gigabytes as appropriate. Here is the body section:

$body += "Processor speed " + $processor.CurrentClockSpeed + "<br>"
$body += "Video ram (meg):  " + $video[0].AdapterRAM/1mb + "<br>"
$body += "Free disk space(gig): " + $disk.FreeSpace/1gb + "<br>"
$body += "Make: " + $system.Manufacturer + "<br>"
$body += "Model: " + $system.Model + "<br>"
$body += "Ram(meg): " + $system.TotalPhysicalMemory/1mb

This is where we add the style information for the body color, and set the H1 heading for the page. This is basic HTML type of code and is not special for Windows PowerShell at all. Here it is:

$head =  "<style>"
$head += "BODY{background-color:peachpuff}"
$head += "</style>"
$head += "<h1>System report for $computer </h1><br>"

To create the actual Web page, we use the ConvertTo-Html cmdlet. We give it the body information from the $body variable and the head information from the $head variable, and we then pipeline the text to the out-File cmdlet where we specify the path and file. We use the $computer variable to create the file name. This is seen here:

ConvertTo-Html  -body $body -Head $head |
Out-File -FilePath "C:\fso\$computer.html"

When we run the script, we generate the following HTML; you will note, no doubt, that it looks like HTML code with all the little tags and the like:

Image of the .html file

 

If we open the file in Internet Explorer (by double-clicking it if Internet Explorer is your default browser), you are greeted with this colorful, cheerful Web page. There is certainly room for improvement, but it does add a bit of color to an otherwise gloomy day:

Image of the .html file rendered in Internet Explorer

 

JE, we hope you have a colorful day. See you tomorrow.

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • great but how do i get video graphics card cpu clock speed?

  • There doesn't appear to be any managed code for that information that I could find. The closest I see is to use the Win32_VideoController Class and get the VideoProcessor property, which at least tells you the GPU type.

    $video = Get-WmiObject -Class Win32_VideoController -ComputerName <whatever>

    $video.VideoProcessor