Skip to content Skip to sidebar Skip to footer

Powershell Html Tables

I am taking some information via Get-WmiObject: $logicalDisks = Get-WmiObject -ComputerName $cmpSys Win32_LogicalDisk Then creating some very basic HTML code to display it drive b

Solution 1:

"Table" is the default output format of the ConvertTo-Html cmdlet:

gwmi Win32_LogicalDisk -Computer $cmpSys -Filter 'DriveType = 3' |
  select @{n='DriveLetter';e={$_.DeviceID -replace ':'}},
         @{n='VolumeLabel';e={$_.VolumeName}},
         @{n='FileSystemType';e={$_.FileSystem}},
         @{n='DiskSize';e={"{0}GB" -f [int]($_.Size/1GB)}},
         @{n='Freespace %';e={"{0}%" -f [int]($_.FreeSpace/$_.Size*100)}} |
  ConvertTo-Html -Head '<style>th,td {text-align:center;}</style>'

If you want to run this agains multiple computers (-Computer can take an array of hostnames), you may want to include the hostname as well:

gwmi Win32_LogicalDisk -Computer $cmpSys -Filter 'DriveType = 3' |
  select @{n='Hostname';e={$_.SystemName}},
         @{n='DriveLetter';e={$_.DeviceID -replace ':'}},
         @{n='VolumeLabel';e={$_.VolumeName}},
         @{n='FileSystemType';e={$_.FileSystem}},
         @{n='DiskSize';e={"{0}GB" -f [int]($_.Size/1GB)}},
         @{n='Freespace %';e={"{0}%" -f [int]($_.FreeSpace/$_.Size*100)}} |
  ConvertTo-Html -Head '<style>th,td {text-align:center;}</style>'

Solution 2:

This is how I accomplish the task of creating an HTML from PowerShell.

# First build the HTML structure for the ConvertTo-HTML$CD_a = $CD_a + "<!DOCTYPE html>"$CD_a = $CD_a + "<html>"$CD_a = $CD_a + "<head>"$CD_a = $CD_a + "<style>"$CD_a = $CD_a + "BODY{background-color:white;}"$CD_a = $CD_a + "TABLE{border=1;border-color: black;border-width: 1px;border-style:solid;border-collapse: separate;empty-cells:show}"$CD_a = $CD_a + "TH{border-width:1px;padding: 3px;border-style:solid;font-weight:bold;text-align:center;border-color:black;background-color:#99CC00}"$CD_a = $CD_a + "TD{color:black;colspan=1;border-width:1px;padding:1px;font-weight:normal;font-size:18;border-style:solid;border-color:black;background-color:#CCFFCC}"$CD_a = $CD_a + "</style>"$CD_a = $CD_a + "</head>"$CD_a = $CD_a + "</html>"# Building the WMI. Change the default name to add spaces.$GWMI_OS = GWmi Win32_operatingsystem
$Start_HTML_OS = $GWMI_OS | select Caption,`
@{name="Number of Users";Expression={$_.NumberOfUsers}},`
@{name="Serial Number";Expression={$_.SerialNumber}},`
@{name="Service Pack Major Version";Expression={$_.ServicePackMajorVersion}},`
@{name="OS Architecture";Expression={$_.OSArchitecture}}

# This is a more specific way and can be customized more with HTML or CSS$GWMI_OS_caption = $GWMI_OS.caption
$GWMI_OS_NumberOfUsers = $GWMI_OS.NumberOfUsers
$GWMI_OS_SerialNumber = $GWMI_OS.SerialNumber
$GWMI_OS_ServicePackMajorVersion = $GWMI_OS.ServicePackMajorVersion
$GWMI_OS_OSArchitecture = $GWMI_OS.OSArchitecture

# Converting the WMI to HTML$Start_HTML_OS_Final = $Start_HTML_OS | ConvertTo-HTML -head $CD_a# Now I create a variable to store the HTML doc.$Create_HTML_doc = "<!DOCTYPE html>
<head><title> Computer Data </title>
<style>
TABLE{border=1; border-color:black; border-width:1px; border-style:solid; empty-cells:show}
TH{border-width:1px; padding:1px; border-style:solid; font-weight:normal; text-align:center;border-color:black;background-color:#99CC00;empty-cells:show}
TD{color:black; colspan=1; border-width:1px; padding:1px; font-weight:bold; font-size:18;border-style:solid;border-color:black;background-color:#CCFFCC;empty-cells:show}
</style>
</head>

<H2> Operating System, WMI </H2> $Start_HTML_OS_Final

<H2> Operating System, WMI </H2>
<table><tr>
<th>  Caption  </th>
<th>  Number Of Users  </th>
<th>  Serial Number  </th>
<th>  Service Pack Major Version </th>
<th>  OS Architecture  </th>
</tr><tr>
<td>  $GWMI_OS_caption  </td>
<td>  $GWMI_OS_NumberOfUsers  </td>
<td>  $GWMI_OS_SerialNumber  </td>
<td>  $GWMI_OS_ServicePackMajorVersion  </td>
<td>  $GWMI_OS_OSArchitecture  </td>
</tr></table>"$Create_HTML_doc > C:\PowerShell\print\HTML\Get_WMI_OS.html
ii C:\PowerShell\print\HTML\Get_WMI_OS.html

PowerShell doesn't have an issue with HTML or CSS. The only consideration you have to make is with escaping the quotation marks "". This is accomplished by using $('"quotation marks"').

Post a Comment for "Powershell Html Tables"