$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 = "
$percentUsage%
" $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 += "$($computer.ToUpper())" $newHtmlFragment += $htmlFragment[2].Replace('',"") $diskData = $htmlFragment[3..($htmlFragment.count -2)] for ($i = 0; $i -lt $diskData.Count; $i++) { if ($($i % 2) -eq 0) { $newHtmlFragment += $diskData[$i].Replace('',"") } else { $newHtmlFragment += $diskData[$i].Replace('',"") } } $newHtmlFragment += $htmlFragment[-1] } $newHtmlFragment = $newHtmlFragment.Replace("usage ", "") $newHtmlFragment = $newHtmlFragment.Replace("usage ", "") $newHtmlFragment = $newHtmlFragment.Replace('<', '<') $newHtmlFragment = $newHtmlFragment.Replace('>', '>') $newHtmlFragment = $newHtmlFragment.Replace(''', "'") $html = @" Disk Usage Report
Disk Usage Disk Utilization Report

Generated on: $((Get-Date).DateTime)
$newHtmlFragment "@ $html > test.html $from = 'no-reply@vcloud-lab.com' $to = 'Patrick.Henighem@vcloud-lab.com' $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