Powershelll Script Output to HTML?

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

LVL 1
Brent387Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

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
 
Chris DentPowerShell DeveloperCommented:

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
 
Brent387Author Commented:
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
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
Brent387Author Commented:
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
 
rwskasCommented:
Are you trying to replace all the excel stuff with an HTML output?
0
 
Brent387Author Commented:
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
 
rwskasCommented:
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
 
Brent387Author Commented:
Definitely. I just need  a starting point.
0
 
rwskasCommented:
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
 
Brent387Author Commented:
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
 
rwskasCommented:
That sounds like a much better solution.
0
 
rwskasCommented:
To get your script to open the newly created HTML file you can use invoke-item

Invoke-Item C:\temp\myhtmlfile.html
0
 
Brent387Author Commented:
Sorry, but where should I add the path into the script for the HTML file?
0
 
Chris DentPowerShell DeveloperCommented:

In mine?

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


$MainFile > "YourHtmlFile.html"


Chris
0
 
rwskasConnect With a Mentor Commented:
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
 
Brent387Author Commented:
Thank  you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.