none
Powershell IF true RRS feed

  • Question

  • I will be creating a scheduled task to run against hosts in my env to notify a specified user when a drive has less than 10 gb of space left. I needed help with the IF true send email to statement. Here is the command I put together. ANy help would be greatly appreciated

    Get-Volume | Where-Object {([math]::truncate($_.SizeRemaining / 1GB) -lt 10)-and ($_.DriveType -eq “FIXED”) -and ($_.FileSystemLabel -ne “System Reserved”)}


    • Edited by sho7x Wednesday, September 11, 2019 8:19 PM
    Wednesday, September 11, 2019 8:19 PM

Answers

  • $mailprops = @{
        To = 'Recepient@something.com'
        From = 'Sender@something.com'
        Subject = 'your email subject'
        SmtpServer = 'SMTP SERVER' 
        BodyAsHtml = $true
    }
    if($data = Get-Volume |
        Where-Object {
            $_.SizeRemaining -lt 10GB -and
            $_.DriveType -eq 'FIXED' -and
            $_.FileSystemLabel -notmatch 'System Reserved|Recovery'
        }){
        $body = $data | ConvertTo-Html | Out-String
        Send-MailMessage @mailprops -Body $body
        
    }


    \_(ツ)_/

    • Marked as answer by sho7x Thursday, September 12, 2019 7:03 PM
    Thursday, September 12, 2019 6:00 PM

All replies

  • What? You forgot your middle school arithmetic already.

    Get-Volume | 
        Where-Object { 
                $_.SizeRemaining -lt 10GB -and 
                $_.DriveType -eq 'FIXED' -and 
                $_.FileSystemLabel -notmatch 'System Reserved|Recovery'
        }


    \_(ツ)_/

    Wednesday, September 11, 2019 9:43 PM
  • Thanks for your prompt response. I apologize if this may seem basic but I have not had much experience with ps. If you could kindly help me with the syntax for an if true then send email to specified email address.
    Thursday, September 12, 2019 2:58 PM
  • Try below:
    $Volume_Data = Get-Volume | 
                    Where-Object { 
                            $_.SizeRemaining -lt 10GB -and 
                            $_.DriveType -eq 'FIXED' -and 
                            $_.FileSystemLabel -notmatch 'System Reserved|Recovery'
                    } | ConvertTo-Html
    
    if($Volume_Data){
        Send-MailMessage -To Recepient@something.com -From Sender@something.com -Body $Volume_Data -SmtpServer "SMTP SERVER" -Subject "Email Subject" -BodyAsHtml
    }

    Thursday, September 12, 2019 3:05 PM
  • Thanks a lot man for taking time to help out. I got this error when I ran it.

    Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
    At line:9 char:70
    + ...  -To x -From y -Body $Volume_Data -SmtpSe ...
    +                                                      ~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

    Thursday, September 12, 2019 5:39 PM
  • $mailprops = @{
        To = 'Recepient@something.com'
        From = 'Sender@something.com'
        Subject = 'your email subject'
        SmtpServer = 'SMTP SERVER' 
        BodyAsHtml = $true
    }
    if($data = Get-Volume |
        Where-Object {
            $_.SizeRemaining -lt 10GB -and
            $_.DriveType -eq 'FIXED' -and
            $_.FileSystemLabel -notmatch 'System Reserved|Recovery'
        }){
        $body = $data | ConvertTo-Html | Out-String
        Send-MailMessage @mailprops -Body $body
        
    }


    \_(ツ)_/

    • Marked as answer by sho7x Thursday, September 12, 2019 7:03 PM
    Thursday, September 12, 2019 6:00 PM
  • thanks boss!
    Thursday, September 12, 2019 7:07 PM
  • It works great, however, the hostname does not show up in the body of the email. If I can get the hostname to be included it will be perfect. This is the current output that is shown in the body:

    HealthStatus

    DriveType

    DriveLetter

    FileSystem

    FileSystemLabel

    ObjectId

    Path

    Size

    SizeRemaining

    PSComputerName

    CimClass

    CimInstanceProperties

    CimSystemProperties

    Healthy

    Fixed

    C

    NTFS

    Volume{7239b817-

    42580570112

    11679776768

    ROOT/Microsoft/Windows/Storage:MSFT_Volume

    Microsoft.Management.Infrastructure.Internal.Data.CimPropertiesCollection

    Microsoft.Management.Infrastructure.CimSystemProperties

     

    Thursday, September 12, 2019 8:06 PM
  • "PsComputerName" is the host name.  You need to add a select statement to reduce the columns to the ones you want.  Remove all unnecessary colmns and order them in the order you want.

    To learn basic PowerShell Start here: Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Thursday, September 12, 2019 8:36 PM