Search code examples
htmlpowershellwmi

Powershell get disk space, convert to HTML and compare a value and if it is less than 15% change the color of the entire row


i have created a PowerShell script that reads from a list of servers, use the Get-WmiObject to get the drive capacity, freespace and then calculates the free space percentage and the converts it all to a nice little HTML.

the question is that i need to figure out how to have it look at the Percentage of free space and if it is below 15% color the entire row of the html table red.

is this even possible? can anyone help with this?

below is my powershell

$servers = GC XX:\myservers.txt
$date = get-date -Format yyyyMMdd
$time = get-date -Format hhmm
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: separate;width:800px}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:lightblue}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:White}"
$a = $a + "</style>"
Foreach ($s in $servers)  
{  
  Get-WmiObject -Class win32_volume -cn $s | 
   Select-Object @{LABEL='Computer';EXPRESSION={$s}}, 
     driveletter, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)}},
       @{LABEL='Capacity';EXPRESSION={"{0:N2}" -f ($_.capacity/1GB)}}, 
        @{LABEL='Percentage';EXPRESSION={"{0:N2}" -f ($_.freespace/$_.capacity*100)+"%"}} | 
     ConvertTo-Html -head $a | Out-File -append "D:\Users\PLACE\Desktop\diskspace\Freespace$date-$time.htm"
} 

Solution

  • here is what i ended up with and it works exactly like i need it to

    $server_file = "D:\location\Windowsservers.txt"
    $html_file_dir = "\\netwolocation\DiskSpace"
    
    
    $background_color = "#FFFFFF" # can be in rgb format (rgb(140,166,193)) or hex format (#FFFFFF)
    $server_name_font = "Tahoma"
    $server_name_font_size = "20px"
    $server_name_bg_color = "#FFFFFF" # can be in rgb format (rgb(77,108,145)) or hex format (#FFFFFF)
    $heading_font = "Tahoma"
    $heading_font_size = "14px"
    $heading_name_bg_color = "#0099FF" # can be in rgb format (rgb(95,130,169)) or hex format (#FFFFFF)
    $data_font = "Tahoma"
    $data_font_size = "11px"
    
    $Critical_space = "#FF0000" # (Red) Critical low space
    $very_low_space = "#FFFF00" # (Yellow) very low space
    $low_space = "#FF9900" # (Orange) low space 
    $medium_space = "#99CC66" # (Green) medium 
    
    $ErrorActionPreference = "SilentlyContinue"
    $date = Get-Date -UFormat "%Y%m%d-%H%m"
    $html_file = New-Item -ItemType File -Path "$html_file_dir\WindowsServerDiskSpaceCheck_$date-$time.html" -Force
    
    $html_file
    
    Function ConvertBytes {
    param($size)
    If ($size -lt 1MB) {
        $drive_size = $size / 1KB
        $drive_size = [Math]::Round($drive_size, 2)
        [string]$drive_size + ' KB'
    }elseif ($size -lt 1GB){
        $drive_size = $size / 1MB
        $drive_size = [Math]::Round($drive_size, 2)
        [string]$drive_size + ' MB'
    }ElseIf ($size -lt 1TB){ 
        $drive_size = $size / 1GB
        $drive_size = [Math]::Round($drive_size, 2)
        [string]$drive_size + ' GB'
    }Else{
        $drive_size = $size / 1TB
        $drive_size = [Math]::Round($drive_size, 2)
        [string]$drive_size + ' TB'
    }
    }
    
    $html_header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Server Drive Space</title>
    <style type="text/css">
    .serverName { text-align:center; font-family:"' + $server_name_font + '"; font-size:' + $server_name_font_size + `
        '; font-weight:bold; background-color: ' + $server_name_bg_color + '; border: 1px solid black; width: 150px; }
    .headings { text-align:center; font-family:"' + $heading_font + '"; font-size:' + $heading_font_size + `
        '; font-weight:bold; background-color: ' + $heading_name_bg_color + '; border: 1px solid black; width: 150px; }
    .data { font-family:"' + $data_font + '"; font-size:' + $data_font_size + '; border: 1px solid black; width: 150px; }
    #dataTable { border: 1px solid black; border-collapse:collapse; }
    body { background-color: ' + $background_color + '; }
    #legend { border: 1px solid black; position:absolute; right:500px; top:10px; }
    </style>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function zxcWWHS(){
     if (document.all){
      zxcCur=''hand'';
      zxcWH=document.documentElement.clientHeight;
      zxcWW=document.documentElement.clientWidth;
      zxcWS=document.documentElement.scrollTop;
      if (zxcWH==0){
       zxcWS=document.body.scrollTop;
      zxcWH=document.body.clientHeight;
       zxcWW=document.body.clientWidth;
      }
     }
     else if (document.getElementById){
      zxcCur=''pointer'';
      zxcWH=window.innerHeight-15;
      zxcWW=window.innerWidth-15;
      zxcWS=window.pageYOffset;
     }
     zxcWC=Math.round(zxcWW/2);
     return [zxcWW,zxcWH,zxcWS];
    }
    
    
    window.onscroll=function(){
     var img=document.getElementById(''legend'');
     if (!document.all){ img.style.position=''fixed''; window.onscroll=null;     return; }
     if (!img.pos){ img.pos=img.offsetTop; }
     img.style.top=(zxcWWHS()[2]+img.pos)+''px'';
    }
    //-->
    </script>
    </head>
    <body>'
    
       $html_footer = '</body>
    </html>'
    
    Add-Content $html_file $html_header
    
    Get-Content $server_file |`
        ForEach-Object { 
            $hostname = Get-WmiObject -Impersonation Impersonate -ComputerName $_ -Query "SELECT Name From Win32_ComputerSystem"
        $name = $hostname.Name.ToUpper()
        Add-Content $html_file ('<Table id="dataTable"><tr><td colspan="3" class="serverName">' + $name + '</td></tr>
        <tr><td class="headings">Drive Letter</td><td class="headings">Total Size</td><td class="headings">Free Space</td><td class="headings">Percent Free</td></tr>')
    
        $drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $_ -Impersonation Impersonate
    
    
        ForEach ($drive in $drives) {
            $space_color = ""
            $free_space = $drive.FreeSpace
            $percent = ($drive.FreeSpace/$drive.size*100)
            $percent = [Math]::Round($percent, 2)
            If ($percent -le 1) {
                $space_color = $Critical_space
            }elseif ($percent -le 5) {
                $space_color = $very_low_space
            }elseif ($percent -le 10) {
                $space_color = $low_space
            }elseif ($percent -le 15) {
                $space_color = $medium_space
            }
    
    
            Add-Content $html_file ('<tr><td class="data">' + $drive.deviceid + '</td><td class="data">' + (ConvertBytes $drive.size) + `
                '</td><td class="data" bgcolor="' + $space_color + '">' + (ConvertBytes $drive.FreeSpace) + '</td><td class="data" bgcolor="' + $space_color + '">' + $percent + '</td></tr>')
        }
    
        Add-Content $html_file ('</table></br><div id="legend">
            <Table><tr><td style="font-size:12px">Less then or equal to 1% Free Space</td><td bgcolor="' + $Critical_space + '" width="10px"></td></tr>
                <tr><td style="font-size:12px">1% to 5% Free Space</td><td bgcolor="' + $very_low_space + '" width="10px"></td></tr>
                <tr><td style="font-size:12px">5% to 10% Free Space</td><td bgcolor="' + $low_space + '" width="10px"></td></tr>
                <tr><td style="font-size:12px">10% to 15% Free Space</td><td bgcolor="' + $medium_space + '" width="10px"></td></tr>
            </table></div>')
        }
    
    
    Add-Content $html_file $html_footer