Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Send system disk space utilization HTML report Email using PowerShell

This script generates system disk drive usage HTML reports using PowerShell. This is very handy for sending weekly or daily reports of servers in html or host it on web server for monitoring. In my case VDI team contacted me for creating a system Disk HTML reports for VDI and sending them to concerned VDI users as notification emails.

After scheduling and deployment of script, below is the screenshot of the Disk Utilization Report email example.

Send system disk space utilization HTML report Email using PowerShell Microsoft outlook email exchange server system desktop configuration vdi desktop hard disk freespace total used space.jpg

To fetch disk reports from multiple computers add their names in computers variable on line number 1. To send the email to concerned users add information on line 131 to 136.

Download this script DiskReport.ps1 here or it is also available on github.com/janviudapi.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
$computers = $env:COMPUTERNAME, 'testmachine2', 'ad001' #, '192.168.34.44'

function Get-DiskDetails
{
    [CmdletBinding()]
    param (
        [string]$Computer = $env:COMPUTERNAME
    )
    $cimSessionOptions = New-CimSessionOption -Protocol Default
    $query = "SELECT DeviceID, VolumeName, Size, FreeSpace FROM Win32_LogicalDisk WHERE DriveType = 3"
    $cimsession = New-CimSession -Name $Computer -ComputerName $Computer -SessionOption $cimSessionOptions
    Get-CimInstance -Query $query -CimSession $cimsession 
}

$newHtmlFragment = [System.Collections.ArrayList]::new()
foreach ($computer in $computers)
{
    $disks = Get-DiskDetails -Computer $computer 
    $diskinfo = @()
    foreach ($disk in $disks) {
        [int]$percentUsage = ((($disk.Size - $disk.FreeSpace)/1gb -as [int]) / ($disk.Size/1gb -as [int])) * 100  #(50/100).tostring("P")
        $bars = "<div style='background-color: DodgerBlue; height: 18px; width: $percentUsage%'><span>$percentUsage%</span></div>" 
        $diskInfo += [PSCustomObject]@{
            Volume = $disk.DeviceID
            VolumeName = $disk.VolumeName
            TotalSize_GB = $disk.Size / 1gb -as [int]
            UsedSpace_GB = ($disk.Size - $disk.FreeSpace)/1gb -as [int]
            FreeSpace_GB = [System.Math]::Round($disk.FreeSpace/1gb)
            Usage = "usage {0}"  -f $bars #, $percentUsage
        }
    }
    $htmlFragment = $diskInfo | ConvertTo-Html -Fragment
    $newHtmlFragment += $htmlFragment[0]
    $newHtmlFragment += "<tr><th class='ServerName' colspan='4'>$($computer.ToUpper())</th></tr>"
    $newHtmlFragment += $htmlFragment[2].Replace('<th>',"<th class='tableheader'>")

    $diskData =  $htmlFragment[3..($htmlFragment.count -2)]
    for ($i = 0; $i -lt $diskData.Count; $i++) {
        if ($($i % 2) -eq 0)
        {
            $newHtmlFragment += $diskData[$i].Replace('<td>',"<td class='td0'>")
        }
        else 
        {
            $newHtmlFragment += $diskData[$i].Replace('<td>',"<td class='td1'>")
        }
    }
    $newHtmlFragment += $htmlFragment[-1]
}
$newHtmlFragment = $newHtmlFragment.Replace("<td class='td0'>usage ", "<td class='usage'>")
$newHtmlFragment = $newHtmlFragment.Replace("<td class='td1'>usage ", "<td class='usage'>")
$newHtmlFragment = $newHtmlFragment.Replace('&lt;', '<')
$newHtmlFragment = $newHtmlFragment.Replace('&gt;', '>')
$newHtmlFragment = $newHtmlFragment.Replace('&#39', "'")

$html = @"
<html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <title>Disk Usage Report</title>
        <style>
            body {
                font-family: Calibri, sans-serif, 'Gill Sans', 'Gill Sans MT', 'Trebuchet MS';
                background-color: whitesmoke;
            }
            .mainhead {
                margin: auto;
                width: 100%;
                text-align: center;
                font-size: xx-large;
                font-weight: bolder;
            }
            table {
                margin: 10px auto;
                width: 70%;
            }
            .ServerName {
                font-size: x-large;
                margin: 10px;
                text-align: left;
                padding: 10 0;
                color: DodgerBlue;
            }
            .tableheader {
                background-color: black;
                color: white;
                padding: 10px;
                text-align: left;
                /* font-size: large; */
                border-bottom-style: solid;
                border-bottom-color: darkgray;
            }
            td {
                background-color: white;
                border-bottom: 1px;
                border-bottom-style: solid;
                border-bottom-color: #404040;
            }

            .usage {
                background-color: SkyBlue ;
                width: 70%;
                color:  black;
            }

            span {
                color: black;
            }

            .td1 {
                background-color: #F0F0F0;
            }
        </style>
    </head>
    <body>
        <div class='mainhead'>
            <img style='vertical-align: middle;' src='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAyADIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQA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nnZptfNK3zPP/wBr648U3XgGz0Pw3ot9qkOpzkX0tjA0zJGmGVCqgkbm5zjHyY71H4+/a0n1b9kOX4KN+zdquheHrLTY0h1mS9lJtLiIiQ3hDWQGS4Zm+YEh3G4ZzXc6D+0L8Otf01LyHxbptorfehv5hbyqcAkFXwTjOMjI9Ca838VeK9b/AGtPFNn8IfhDaPrB1NkbU9YaJkt4IFYMzMWAKRqQCzEZY4Vclhn77iTC5fiebMJ4q7taMVZ/JeV9W+h+Q8E4/OcDyZPSwHKuZuc5KUbXerd1a6WiXW3qzyrwr/wUE+MXg3wvo/h/TNW06PTdJs4bC1STTo2ZYokCICT1O1RzRX7SeCf2N/ht4R8F6BoUmhWWoyaXp9vZNeXFlbtJOY41QyMfL5ZtuSfU0V+Tn9DHuQ7fWjutFFACL/D+NRXdnb6haSW11BHc20qlZIZkDo4PUEHgiiigD+fP/goV4U0TwX+0tqumeH9H0/QtNWws3Wz021S3hDNECxCIAMk8k45r9of2O/A/hzwf8E9Bl0Hw/peiSXtnaz3T6dZR25nkNrCS7lFG5ie55oooA9xAGBxRRRQB/9k=' alt='Disk Usage' width='50' height='50'/>
            Disk Utilization Report
        </div>
        <br>
        <div><i><b>Generated on: </b> $((Get-Date).DateTime)</i></div>
        $newHtmlFragment
    </body>
</html>
"@

$html > test.html

$from = '[email protected]'
$to = '[email protected]'
$subject = 'Weekly disk utilization report'
$body = $html
$smtpServer = 'emailexchange.vcloud-lab.com'
$smtpPort = 587

Send-MailMessage -From $from -to $to -Subject $Subject -Body $body -BodyAsHtml -SmtpServer $smtpServer -Port $smtpPort

Useful Articles
Executing PowerShell script from PHP HTML web server
Powershell GUI encode decode images
Powershell GUI format text on TextBox and RichTextBox
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out
Powshell GUI Date and Time converter tool
Powershell adding leading zeros to string or int
PowerShell convert string to base64 value
PowerShell Encode or Decode an WebURL
Create an interactive HTML report with PowerShell data

Go Back



Comment

Blog Search

Page Views

12056342

Follow me on Blogarama