How to trigger email alert when the Disk Space is less than 20%

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 1 year ago.

  • Author
  • #47895
    Profile photo of pavan midde
    pavan midde


    I want to trigger an email alert when the diskspace for the different servers will be less than 20 percent.
    The below given code will generate Excel sheet as an output and provides the diskspace data in it.

    So please suggest me on how to get it done.

    Output in Excel sheet
    Machine Name Drive Total size (GB) Free Space (GB) Free Space (%)
    MyMachine C: 50 11 23%
    D: 100 92 92%

    #Parameter Declareation

    $erroractionpreference = “SilentlyContinue”
    $objExcel = New-Object -comobject Excel.Application
    $objExcel.visible = $True
    $objWorkbook = $objExcel.Workbooks.Add()
    $objWorkSheet = $objWorkbook.Worksheets.Item(1)
    $objWorkSheet.Cells.Item(1,1) = “Machine Name”
    $objWorkSheet.Cells.Item(1,2) = “Drive”
    #$objWorkSheet.cells.item(1,2) = "Volume Name "
    $objWorkSheet.Cells.Item(1,3) = “Total size (GB)”
    $objWorkSheet.Cells.Item(1,4) = “Free Space (GB)”
    $objWorkSheet.Cells.Item(1,5) = “Free Space (%)”

    $Header = $objWorkSheet.UsedRange
    $Header.Interior.ColorIndex = 09
    $Header.Font.ColorIndex = 19
    $Header.Font.Bold = $True
    $intRow = 2

    $objWorkSheetomputers = Get-Content $FilePath
    $objWorkSheetomputers = Get-Content E:\Servers.txt

    Write-Host "Input FilePath: " $FilePath
    Write-Host "Processing........."

    $Date = Get-Date -UFormat "%B_%d_%Y"
    $curRow = $intRow
    $endRow = $intRow

    foreach ($strComputer in $objWorkSheetomputers)
    $objWorkSheet.Cells.Item($intRow, 1) = $strComputer.ToUpper()
    Write-Host "Server:" $strComputer ": Processing..."

    $objWorkSheetolDisks = Get-WmiObject Win32_LogicalDisk -computername $strComputer -Filter “DriveType = 3"

    if ($objWorkSheetolDisks -ne $null)
    $curRow = $intRow
    foreach ($objDrive in $objWorkSheetolDisks)
    $objWorkSheet.Cells.Item($intRow, 2) = $objDrive.DeviceID
    #$objWorkSheet.cells.item($introw, 3) = $objDrive.volumename
    $objWorkSheet.Cells.Item($intRow, 3) = “{0:N0}” -f ($objDrive.Size/1GB)
    $objWorkSheet.Cells.Item($intRow, 4) = “{0:N0}” -f ($objDrive.FreeSpace/1GB)
    $objWorkSheet.Cells.Item($intRow, 5) = “{0:P0}” -f ([double]$objDrive.FreeSpace/[double]$objDrive.Size)
    $intRow = $intRow + 1
    $endRow = $intRow – 1
    $RowMergeCells = $objWorkSheet.Range("A${curRow}:A$endRow")
    $RowMergeCells.MergeCells = $true
    $RowMergeCells.VerticalAlignment = -4108

    throw $Error[0].exception


    $err = $_.Exception
    Write-Host "Error in " $servername " :: " $err

    $objWorkSheet.cells.item($intRow, 2) = $err
    $MergeCells = $objWorkSheet.Range("B${intRow}:E$intRow")
    $MergeCells.MergeCells = $true
    $MergeCells.Interior.ColorIndex = 3

    $intRow = $intRow + 1

    $temDir = "C:\temp"
    if ((Test-Path $temDir) -eq 0)
    mkdir $temDir | Out-Null

    $a = Get-Date
    $strComputer + "::" + $a.ToLongTimeString() + ":: $_" + "`r`n" | Out-File c:\temp\ServerDriversDetails_Logs-$Date.txt -Append

    $intRow = $intRow + 1
    Write-Host "Server:" $strComputer ": Processing Completed."

    #Auto fit everything so it looks better
    $usedRange = $objWorkSheet.UsedRange
    $usedRange.EntireColumn.AutoFit() | Out-Null

    Write-Host "Process Completed"
    Read-Host "Press any key to exit"


  • #47916
    Profile photo of Mark Prior
    Mark Prior

    so you just want a way to email the content this generates automatically?

    if so:

    $smtpServer = ""
    $From = ""
    $Email = ""
    $SubjectCPY = "Disk Below 20%"
    $Body = "$mailcontent"
    #Email Content - (will only send if $mailcontent has a value, you could also use test path to validate or date modified that part is up to you, for this you will need to assign $mailcontent value to the action above & then output that to a file after.)
    if ($mailcontent) {Send-Mailmessage -smtpServer $smtpServer -from $from -to $Email -subject $subjectCPY -priority High -Body $Body -Attachments "c:\temp\ServerDriversDetails_Logs-$Date.txt"}
    • This reply was modified 1 year ago by Profile photo of Mark Prior Mark Prior.
  • #47921
    Profile photo of pavan midde
    pavan midde

    1.It should check the diskspace which is less than 20%
    2.The generated excel sheet should be attached in the email thread, showing which servers are affected with less disk space.

  • #47927
    Profile photo of Rob Simmers
    Rob Simmers

    This is a very common question and there are thousands of iterations of a solution to gather and report on diskspace. Using Excel is an antiquated way of doing reporting and especially for something like diskspace on a single server, there's nothing to analyze or sort, so exporting to Excel is expensive just to get a simple report. Typically, you are going to see this kind of reporting as HTML which can be used directly as the body in the email, take a look at this:

    If you don't like this solution per se, you should just do some searching and you will find a lot of examples.

You must be logged in to reply to this topic.