Powershell HTML Tables

2019-08-24 02:34发布

问题:

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 by drive:

Foreach ($disk in $logicalDisks) {
If ($disk.DriveType -eq 3) {
$disksize = [math]::round(($disk.size / 1048576))
$freespace = [math]::round(($disk.FreeSpace / 1048576))
$percFreespace=[math]::round(((($disk.FreeSpace / 1048576)/($disk.Size / 1048676)) * 100),0)
$body += @"
    <font color="red">Drive Letter: </font>$($disk.DeviceID)
    <br>
    <font color="red">Volume Label: </font>$($disk.VolumeName)
    <br>
    <font color="red">FileSystem Type: </font>$($disk.FileSystem)
    <br>
    <font color="red">Disk Size (MB): </font>$($disksize)MB
    <br>
    <font color="red">Free Space (MB) / %: </font>$($freespace)MB / $($percFreeSpace)%
    <br>
    <br>
"@
    }
}

However, this display is fairly generic, and I would like a usable report to pass on to other departments. How could I format it in a table? Something like:

DriveLetter    VolumeLabel    FileSystemType    DiskSize   Freespace %
     C             OS             NTFS           100GB         32%
     E            DATA            NTFS           1000GB         2%

回答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>'


回答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"').