Email notification based on Disk Space Percentage RRS feed

  • Question

  • In the below sample script how can i send out an email only when disk space is less than 10%.The below script works fine but it always sends out an e-mail when it executes manually even if the disk space is more than 10%.Can anyone explain what is wrong in the script?

    $DiskReport = ForEach ($Servernamesin ($File))     {Get-WmiObject win32_logicaldisk <#-Credential $RunAccount#> `  -ComputerName $Servernames-Filter"Drivetype=3" `  -ErrorAction SilentlyContinue |#return only disks with#free space less  #than or equal to 0.1 (10%)    Where-Object {   ($_.freespace/$_.size) -le '0.1'}    }       #create reports$DiskReport|Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},  @{Label = "Drive Letter";Expression = {$_.DeviceID}},  @{Label = "Total Capacity (GB)";Expression = {"{0:N1}"-f( $_.Size / 1gb)}},  @{Label = "Free Space (GB)";Expression = {"{0:N1}"-f( $_.Freespace / 1gb ) }},  @{Label = 'Free Space (%)'; Expression = {"{0:P0}"-f ($_.freespace/$_.size)}} |#Export report to CSV file (Disk Report)Export-Csv-path "\\Servername\ServerStorageReport\DiskReport\DiskReport_$logDate.csv"-NoTypeInformation        #Send disk report using the exchange email module      Import-Module "\\Servername\ServerStorageReport\ExchangeModule\Exchange.ps1"-ErrorAction SilentlyContinue   

    # Attach and send CSV report (Most recent report will be attached)$messageParameters = @{                                          Subject = "Weekly Server Storage Report"                                          Body = "Attached is Weekly Server Storage Report. The scipt has been amended to return only servers with free disk space less than or equal to 10%. All reports are located in \\Servername\ServerStorageReport\DiskReport\, but the most recent  is sent weekly"From = "Email name1 <Email.name1@domainname.com>"                                          To = "Email name1 <Email.name1@domainname.com>"                  CC = "Email name2 <Email.name2@domainname.com>"                  Attachments = (Get-ChildItem \\Servername\ServerStorageReport\DiskReport\*.*|sort LastWriteTime |select-last 1)                                     SmtpServer = "SMTPServerName.com"                                      }     Send-MailMessage @messageParameters -BodyAsHtml            

    • Changed type denprash Tuesday, September 10, 2019 7:27 AM
    • Changed type jrv Tuesday, September 10, 2019 7:38 AM
    Tuesday, September 10, 2019 6:30 AM

All replies

  • Please read the following and fix you post accordingly:

    How to post code in Technet Forums

    You also need to correctly format the code so that it is properly indented and readable.  As posted it is badly broken and cannot be easily read or copied.


    Tuesday, September 10, 2019 7:20 AM
  • 10% as a number looks like this;

    Where-Object {$_.freespace/$_.size -le 0.1}

    You cannot use strings for numbers.


    Tuesday, September 10, 2019 7:37 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Friday, October 4, 2019 8:23 AM