Solved

Powershelll Script Output to HTML?

Posted on 2010-08-31
16
651 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Are you trying to replace all the excel stuff with an HTML output?
0
 
LVL 1

Author Comment

by:Brent387
Comment Utility
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
Comment Utility
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
Comment Utility
Definitely. I just need  a starting point.
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Brent387
Comment Utility
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
Comment Utility

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
Comment Utility
That sounds like a much better solution.
0
 
LVL 6

Expert Comment

by:rwskas
Comment Utility
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
Comment Utility
Sorry, but where should I add the path into the script for the HTML file?
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

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
Comment Utility
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
Comment Utility
Thank  you!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

14 Experts available now in Live!

Get 1:1 Help Now