none
Email based on Disk Percentage-Only when low RRS feed

  • Question

  • $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
    In the above 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?


    Tuesday, September 10, 2019 7:36 AM

All replies

  • Hi denprash.

    In the below line

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

    you have to change "-le" to "-lt": "-le" will make the script send the message also if the ratio is equal to 10%.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE


    Tuesday, September 10, 2019 7:55 AM
  • Hi Lugi,

    I have made the changes as you suggested.My disk contains 47% available free space.I wanted to send out an e-mail only when it is equal or less than 10%

    Denprash

    Tuesday, September 10, 2019 8:17 AM
  • Hi Lugi,

    I have made the changes as you suggested.My disk contains 47% available free space.I wanted to send out an e-mail only when it is equal or less than 10%

    Denprash

    In your original question, you wrote "how can I send out an email only when disk space is less than 10%": if you want to check such condition, you have to use "-lt".
    You can use the PowerShell code provided in the post at 
    https://blogs.technet.microsoft.com/parallel_universe_-_ms_tech_blog/2011/11/08/powershell-check-your-free-disk-space-and-warn-below-a-threshold/ as starting point for your script.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Tuesday, September 10, 2019 8:27 AM