JChutey JChutey - 1 month ago 8
HTML Question

Powershell to HTML - I can't add a break/new line between each Server 'Disk Space' info

I have the following Powershell script to query the Uptime/Disk Space/Memory Usage on several remote VM's, but the results that are produced are bunched together and as I add the remaining Servers to this script (about 30/40 odd more to be added) the data starts to become un-readable.

[CmdLetBinding()]
Param ( [Parameter(Mandatory=$false,Position=1)][string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
[Parameter(Mandatory=$false,Position=2)][array]$servers = @("Svr1","Svr2","Svr3")
)

Function Get-UpTime
{ Param ([string[]]$servers)
Foreach ($s in $servers)
{
$os = Get-WmiObject -class win32_OperatingSystem -cn $s
New-Object psobject -Property @{
Uptime = ((get-date) - $os.converttodatetime($os.lastbootuptime)).Days
Hostname=$s
}
}
}

Function Get-DiskSpace
{
Param ([string[]]$servers)
Foreach ($s in $servers)
{
Get-WmiObject -Class win32_volume -cn $s |
Select-Object @{LABEL='Server';EXPRESSION={$s}},
driveletter, label,
@{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}},
@{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
@{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
@{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}
}
}

Function Get-MemoryUsage
{
Param ([string[]]$servers)
Foreach ($s in $servers)
{
Get-WmiObject -Class win32_OperatingSystem -cn $s |
Select-Object @{LABEL='Server';EXPRESSION={$s}},
@{LABEL=" Total Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvisiblememorysize / 1024))}},
@{LABEL=" Free Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.freephysicalmemory / 1024))}},
@{LABEL=" Total Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvirtualmemorysize / 1024))}},
@{LABEL=" Free Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.freevirtualmemory / 1024))}}
}
}

$upTime = Get-UpTime -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
Created on: $(get-date)
<h2>Server Uptime</h2> " | Out-String

$disk = Get-DiskSpace -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
<h2>Disk</h2> "| Out-String

$memory = Get-MemoryUsage -servers $servers |
ConvertTo-Html -As Table -Fragment -PreContent "
<h2>Memory Usage</h2> "| Out-String


$head = @'
<style media='screen'>
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}

table{
border-collapse: collapse;
border: none;
font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
margin-bottom: 10px;
}

table td{
font-size: 12px;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

table th {
font-size: 12px;
font-weight: bold;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

h2{ clear: both; font-size: 130%;color:#354B5E; }

h3{
clear: both;
font-size: 75%;
margin-left: 20px;
margin-top: 30px;
color:#475F77;
}

p{ margin-left: 20px; font-size: 12px; }

table.list{ float: left; }

table.list td:nth-child(1){
font-weight: bold;
border-right: 1px grey solid;
text-align: right;
}

table.list td:nth-child(2){ padding-left: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #BBBBBB; }
table tr:nth-child(odd) td:nth-child(odd){ background: #F2F2F2; }
table tr:nth-child(even) td:nth-child(odd){ background: #DDDDDD; }
table tr:nth-child(odd) td:nth-child(even){ background: #E5E5E5; }
div.column { width: 320px; float: left; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-left: 30px; }
table{ margin-left: 20px; }
–>
</style>
<style media='print'>

</style>
'@

ConvertTo-Html -Head $head -PreContent "<h1>Daily Status Report - SQL Servers</h1>" -PostContent $upTime, $disk, $memory >> $path
Invoke-Item $path


This image below makes it easier to understand. I basically need a 'new line' or 'break' to show between each Server (marked with a 'Red' line), but I can't figure out the Powershell/HTML coding to make this happen.

enter image description here

Answer

The easiest way to accomplish a separation is probably just to add an empty object at the bottom of each collection, Will at the very least give you a line break between each server. so you would add something like this at the bottom of the Get-DiskSpace function

Function Get-DiskSpace 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_volume -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         driveletter, label,
         @{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}}, 
         @{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
         @{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
         @{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}           
}
    [PsCustomObject]@{Server = "";driveletter = "";label = ""; "Total (GB)" = ""; "Free Space (GB)" = ""; "Percent Free (%)" = ""; "Percent Usage (%)" = ""}

}

Note that this is probably not the best way to go as it's hard to extend that methodology to other scripts but it will accomplish what you are looking for.