#current server name $servername = "ROGUE" #if free space % falls below this threshold, #assign CSS class "critical" which makes font red $criticalthreshold = 10 #inline css for styling $inlinecss = @" <style> table { margin: 0px; border: 1px solid #7e7e7e; background-color: #fafafa; border-collapse: collapse; } #every other row has different color tr:nth-child(even) /* doesnt work in IE8 */ { background-color: #d5e4f4; } th, td { width: 100px; text-align: left; } th { background-color:#a6bdd6; font-weight:bold; } #anything marked as critical is styled bold and red .critical, .critical td { color: red; font-weight: bold; } </style> "@ #construct the html content $htmlhead = "<head><title>Disk Space Report </title>$($inlinecss)</head>" $htmlbody = "<body>" $htmlbody += "<p>$($subject)</p>" #below creates table headers $htmlbody += "<table><tbody>" $htmlbody += "<th>Device ID</th>" $htmlbody += "<th>Size (GB)</th>" $htmlbody += "<th>Free Space (GB)</th>" $htmlbody += "<th>Free Space (%)</th>" #table content is dynamically generated from Get-WmiObject #here we extract disk usage #to look only at Local Disk, add filter for DriveType -eq 3 Get-WmiObject -Class Win32_LogicalDisk -ComputerName $servername | ForEach-Object { $disk = $_ $size = "{0:N1}" -f ($disk.Size/1GB) $freespace = "{0:N1}" -f ($disk.FreeSpace/1GB) if ($disk.Size -gt 0) { $freespacepercent = "{0:P0}" -f ($disk.FreeSpace/$disk.Size) } else { $freespacepercent = "" } if ($freespacepercent -ne "" -and $freespacepercent -le $criticalthreshold) { $htmlbody += "<tr class='critical'>" } else { $htmlbody += "<tr>" } $htmlbody += "<td>$($disk.DeviceID)</td>" $htmlbody += "<td>$($size)</td>" $htmlbody += "<td>$($freespace)</td>" $htmlbody += "<td>$($freespacepercent)</td>" $htmlbody += "</tr>" } $htmlbody += "</tbody></table></body></html>" #compose full html content $htmlcontent = $htmlhead + $htmlbody #email settings $currdate = Get-Date -Format "yyyy-MM-dd hmmtt" $smtp = "mail.rogue.local" $to = "DBA <administrator@rogue.local>" $from = "DBMail <dbmail@administrator.local>" $subject = "Disk Space Report for $servername as of $currdate" Send-MailMessage -SmtpServer $smtp -To $to -from $from -Subject $subject -Body "$($htmlcontent)" -BodyAsHtml
Monday, 16 March 2015
Powershell - Disk Space Alert Email
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment