Solved

Powershelll Script Output to HTML?

Posted on 2010-08-31
16
652 Views
Last Modified: 2012-05-10
Hello,

I would like to have this script output its results in HTML format but I can't figure out how to do that using the ConvertTo-HTML cmdlet and I don't know of many other options. I had it save to an html document but it looked really bad. Any thoughts? Also, if there is a way to condense it, that would be much appreciated also. There is a lot of repetition.
##########Declare Computer variable##########



$strComputer = “COMPUTER”



##########Open Excel Application and start new workbook##########



$Excel = New-Object -Com Excel.Application

  $Excel.visible = $True

  $Excel = $Excel.Workbooks.Add()



##########Name the Spreadsheet and begin to populate it for GENERAL INFO##########



$Sheet = $Excel.WorkSheets.Item(1)

     $Sheet.name = "$strComputer"

	$Sheet.Cells.Item(1,1) = “$strComputer Documentation”

	$Sheet.Cells.Item(1,2) = "Date of Documentation:" 

	$Sheet.Cells.Item(1,3) = Get-Date -format "M-dd-yyyy"

	$Sheet.Cells.Item(1,4) = "Server Location:"

	$Sheet.Cells.Item(1,5) = "FAIRFAX"



##########Format the cells for green color##########

   

   $Sheet.Cells.Item(1,1).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,1).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,1).Font.Bold = $True

   $Sheet.Cells.Item(1,2).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,2).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,2).Font.Bold = $True

   $Sheet.Cells.Item(1,3).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,3).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,3).Font.Bold = $True

   $Sheet.Cells.Item(1,4).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,4).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,4).Font.Bold = $True

   $Sheet.Cells.Item(1,5).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,5).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,5).Font.Bold = $True

   $Sheet.Cells.Item(1,6).Interior.ColorIndex = 50

   $Sheet.Cells.Item(1,6).Font.ColorIndex = 20

   $Sheet.Cells.Item(1,6).Font.Bold = $True

  



$intRow = 2



##########Begin to populate the spreadsheet with headers for SYSTEM INFO##########



$intRow = $intRow + 1



     $Sheet = $Excel.WorkSheets.Item(1)

        $Sheet.Cells.Item($intRow,1) = “Computer Manufacturer:”

	$Sheet.Cells.Item($intRow,2) = “Model:”

	$Sheet.Cells.Item($intRow,3) = “Total Memory:”

	$Sheet.Cells.Item($intRow,4) = "Support Number:"

	$Sheet.Cells.Item($intRow,5) = "Express Code:"



##########Change color of the header row to GREEN##########



for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}



$intRow = $intRow + 1



##########Populate the SYSTEM INFO from root\CIMV##########



$colItems = Get-WmiObject Win32_ComputerSystem -Namespace “root\CIMV2" `

-ComputerName $strComputer



   foreach($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.Manufacturer

	$Sheet.Cells.Item($intRow,2) = $objItem.Model

	$Sheet.Cells.Item($intRow,3) = $objItem.TotalPhysicalMemory / 1GB

	$Sheet.Cells.Item($intRow,4) = "1-877-293-1196"

	$Sheet.Cells.Item($intRow,5) = "#33731939701" 

	$Sheet.Cells.Item($intRow,3).NumberFormat = "#.00" 



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }



$intRow = $intRow + 1

}



$intRow = $intRow + 1

     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = "Warranty Type:"

	$Sheet.Cells.Item($intRow,2) = "Expiration Date:"



for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}

$intRow = $intRow + 1

	$Sheet.Cells.Item($intRow,1) = "Gold 4 Hour Support"

	$Sheet.Cells.Item($intRow,2) = "8/12/2010"



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }

$intRow = $intRow + 1



##########Begin to populate the spreadsheet with headers for BIOS INFO##########	

$intRow = $intRow + 1

     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “BIOS:”

	$Sheet.Cells.Item($intRow,2) = “Version:”

	$Sheet.Cells.Item($intRow,3) = “Service Tag:”



##########Change color of the header row to GREEN##########

	

for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}



$intRow = $intRow + 1



$colItems = Get-WmiObject Win32_BIOS -Namespace “root\CIMV2"`

-ComputerName $strComputer



   foreach ($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.Description

	$Sheet.Cells.Item($intRow,2) = $objItem.SMBIOSMajorVersion

	$Sheet.Cells.Item($intRow,3) = $objItem.SerialNumber



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }



$intRow = $intRow + 1

}



##########Begin to populate the spreadsheet with headers for OPERATING SYSTEM##########



$intRow = $intRow + 1

     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “Operating System:”

 for($i = 1; $i -le 6; $i++)

	 {

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

	}

$intRow = $intRow + 1



$colItems =  Get-WmiObject "Win32_OperatingSystem" -Namespace "root/CIMV2" `

-Computername $strComputer



   foreach($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.Name

	 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }



$intRow = $intRow +1

}



##########Begin to populate the spreadsheet with headers for HARD DRIVE INFO##########



$intRow = $intRow + 1

   $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “Hard Drive Name”

	$Sheet.Cells.Item($intRow,2) = “Drive Letter”

	$Sheet.Cells.Item($intRow,3) = “Description”

	$Sheet.Cells.Item($intRow,4) = “FileSystem”

	$Sheet.Cells.Item($intRow,5) = “Size in GB”

	$Sheet.Cells.Item($intRow,6) = “Free Space in GB”



##########Change color of the header row to GREEN##########



for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}



$intRow = $intRow + 1



$colItems = Get-wmiObject -class “Win32_LogicalDisk” -namespace “root\CIMV2" `

-computername $strComputer



   foreach ($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.VolumeName

	$Sheet.Cells.Item($intRow,2) = $objItem.DeviceID

	$Sheet.Cells.Item($intRow,3) = $objItem.Description

	$Sheet.Cells.Item($intRow,4) = $objItem.FileSystem

	$Sheet.Cells.Item($intRow,5) = $objItem.Size / 1GB

	$Sheet.Cells.Item($intRow,6) = $objItem.FreeSpace / 1GB

	   $Sheet.Cells.Item($intRow,5).NumberFormat = "#.00"

	   $Sheet.Cells.Item($intRow,6).NumberFormat = "#.00"



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }





$intRow = $intRow + 1



}



##########Begin to populate the spreadsheet with headers for PROCESSOR INFO##########



$intRow = $intRow + 1



     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “Processor Name:”



##########Change color of the header row to GREEN##########



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }





$intRow = $intRow + 1



$colItems = Get-WmiObject Win32_Processor -Namespace “root\CIMV2" `

-Computername $strComputer



   foreach($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.Name

 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }

 



$intRow = $intRow +1

}



##########Begin to populate the spreadsheet with headers for NETWORK INFO##########



$intRow = $intRow + 1



     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “DHCP Enabled:”

	$Sheet.Cells.Item($intRow,2) = “IP Address:”

	$Sheet.Cells.Item($intRow,3) = “Subnet Mask:”

	$Sheet.Cells.Item($intRow,4) = “Default Gateway:”

	$Sheet.Cells.Item($intRow,5) = “MAC Address:”



##########Change color of the header row to GREEN##########



for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}



$intRow = $intRow + 1

$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace “root\CIMV2" `

-ComputerName $strComputer | where{$_.IPEnabled -eq “True”}



   foreach($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.DHCPEnabled

	$Sheet.Cells.Item($intRow,2) = $objItem.IPAddress

	$Sheet.Cells.Item($intRow,3) = $objItem.IPSubnet

	$Sheet.Cells.Item($intRow,4) = $objItem.DefaultIPGateway

	$Sheet.Cells.Item($intRow,5) = $objItem.MACAddress



 for($i = 1; $i -le 6; $i++)

	 {

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }





$intRow = $intRow +1

}



##########Begin to populate the spreadsheet with headers for iSCSI PERSISTENT TARGET INFO##########



$intRow = $intRow + 1



     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($intRow,1) = “Target Name:”



##########Change color of the header row to GREEN##########

 for($i = 1; $i -le 6; $i++)

	 {

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}

$intRow = $intRow + 1



$colItems = Get-WmiObject MSiSCSIInitiator_PersistentLoginClass -Namespace “root\WMI" `

-ComputerName $strComputer 



   foreach($objItem in $colItems) {

	$Sheet.Cells.Item($intRow,1) = $objItem.TargetName

 for($i = 1; $i -le 6; $i++)

	 {

		$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

}

	 

$intRow = $intRow +1

}



##########Begin to populate the spreadsheet with headers for APPLICATION INFO##########



$intRow = $intRow + 1



     $Sheet = $Excel.WorkSheets.Item(1)

	$Sheet.Cells.Item($introw,1) = “Installed Applications:”

	$Sheet.Cells.Item($introw,2) = “Vendor:”

	$Sheet.Cells.Item($introw,3) = “Version:”

	$Sheet.Cells.Item($introw,4) = “Install Date:”



##########Change color of the header row to GREEN##########

 

for($i = 1; $i -le 6; $i++){

   $Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 50

   $Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 20

   $Sheet.Cells.Item($intRow,$i).Font.Bold = $True

}



$introw = $introw + 1



##########Search registry for installed applications##########



$Keys = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall





##########Do not write any application that contains the words 'Update' or 'Hotfix' in the documentation##########

 

$Items = $keys | foreach-object {Get-ItemProperty $_.PsPath} | where-object {($_.DisplayName -notmatch "Update|Hotfix") -and ($_.DisplayName -ne $null)}

   

   foreach ($item in $items){

	$Sheet.Cells.Item($intRow,1) = $Item.DisplayName

	$Sheet.Cells.Item($intRow,2) = $Item.Publisher

	$Sheet.Cells.Item($intRow,3) = $Item.DisplayVersion

	$Sheet.Cells.Item($intRow,4) = $Item.InstallDate



for($i = 1; $i -le 6; $i++){

	 	$Sheet.Cells.Item($intRow,$i).Interior.ColorIndex = 35;

     		$Sheet.Cells.Item($intRow,$i).Font.ColorIndex = 0;

		$Sheet.Cells.Item($intRow,$i).Font.Bold = $true;

	 }

$intRow = $intRow + 1

}



##########Autofit the used columns in the spreadsheet##########



$Sheet.UsedRange.EntireColumn([A1]) = 60

$Sheet.UsedRange.EntireColumn.AutoFit()

Clear 



$Excel.SaveAs("C:\$strComputer.xlsx")

Open in new window

0
Comment
Question by:Brent387
  • 7
  • 6
  • 3
16 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 33568728

Do you have a version you've tried to output as HTML? It'll take a fair bit of work to convert this one into HTML otherwise (although it's far from impossible).

Chris
0
 
LVL 1

Author Comment

by:Brent387
ID: 33569475
Is there a method you could show me that I can just apply to each section or something? I will try to find my  html attempts.
0
 
LVL 1

Author Comment

by:Brent387
ID: 33569503
I found this online and I have been trying to work with it but I don't know how to make it apply to more than one section or item for that matter.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<style>BODY{Background-color:peachpuff;}TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:thistle;}TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod;}</style>

</head><body>

<H2>Server Documentation</H2>

<table>

<colgroup>

<col/>

<col/>

<col/>

</colgroup>

<tr><th>Description</th><th>SMBIOSMajorVersion</th><th>SerialNUmber</th></tr>

<tr><td>Phoenix ROM BIOS PLUS Version 1.10 A17</td><td>2</td><td>3L9FYF1</td></tr>

</table>

</body></html>

Open in new window

0
 
LVL 6

Expert Comment

by:rwskas
ID: 33571228
Are you trying to replace all the excel stuff with an HTML output?
0
 
LVL 1

Author Comment

by:Brent387
ID: 33571265
I think so. I want it to open in HTML in the chart format but my HTML knowledge is not very extensive so I was hoping to be able to use the powershell that queries WMI and what have you, but also put it into a nice looking table in HTML. Does that make sense?
0
 
LVL 6

Expert Comment

by:rwskas
ID: 33577162
That will basically consist of rewritting the entire script, replacing excel formatting code, with HTML formatting code. It can be done, but based on the amount of code you have there would take some time.

Is it safe to assume that If I did a small section of your original code, that you would be able to propagate it for the rest of your code?
0
 
LVL 1

Author Comment

by:Brent387
ID: 33577629
Definitely. I just need  a starting point.
0
 
LVL 6

Expert Comment

by:rwskas
ID: 33577782
Actually, now that I think about it, it would probably be easier to simplify the script, not make it more complicated.

What are the chances that instead of excel, your script can export to a standard CSV?

Powershell has no issues converting CSVs into table format for HTML, it would simplify your whole process by orders of magnitude.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:Brent387
ID: 33577827
I'm pretty sure it wouldn't be too much to get it to export to csv. My (other) big issue is what to add into the script to make it open in html.
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 33577876

If it's all different information I'd construct it like this:


$OSFragment = Get-WmiObject Win32_OperatingSystem | Select-Object Caption, OSArchitecture | ConvertTo-Html -Fragment
$CompFragment = Get-WmiObject Win32_ComputerSystem | Select-Object Model, Manufacturer | ConvertTo-Html -Fragment

$Body = @()
$Body += "<h1>OS</h1>"
$Body += $OSFragment
$Body += "<h1>Computer</h1>"
$Body += $CompFragment

$MainFile = ConvertTo-Html -Body $Body


You can apply a CSS style to the HTML to make it pretty :)

Chris
0
 
LVL 6

Expert Comment

by:rwskas
ID: 33577899
That sounds like a much better solution.
0
 
LVL 6

Expert Comment

by:rwskas
ID: 33577907
To get your script to open the newly created HTML file you can use invoke-item

Invoke-Item C:\temp\myhtmlfile.html
0
 
LVL 1

Author Comment

by:Brent387
ID: 33577952
Sorry, but where should I add the path into the script for the HTML file?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 33577987

In mine?

If so, a bit of redirection at the end will do:


$MainFile > "YourHtmlFile.html"


Chris
0
 
LVL 6

Assisted Solution

by:rwskas
rwskas earned 250 total points
ID: 33577994
anywhere. If you use Chris's example, you could do

Set-Content -Path "C:\mypath\htmlfile.html" $MainFile
Invoke-Item "C:\mypath\htmlfile.html"

Or you could set the path as a variable somewhere else in the script and then
Set-Content -Path $MyVariable $MainFile
Invoke-Item $MyVariable
0
 
LVL 1

Author Comment

by:Brent387
ID: 33578187
Thank  you!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now