Powershell script to send an email notification with servers with disk space

Welcome Forums General PowerShell Q&A Powershell script to send an email notification with servers with disk space

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 years, 8 months ago.

  • Author
    Posts
  • #49460

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    # Script Disk Space
    $users = “xxxxxxxx” # List of users to email your report to (separate by comma)
    $fromemail = “xxxxxxxxx”
    $server = “xxxxxxx” #enter your own SMTP server DNS name / IP address here
    $list = "C:\Powershell Script\list.txt" #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
    $computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
    $mydate = get-date
    # Set free disk space threshold below in percent (default at 10%)
    [decimal]$thresholdspace = 10

    #assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above.
    $tableFragment= Get-WMIObject -ComputerName $computers Win32_LogicalDisk `
    | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={“{0:n2}” -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={“{0:n2}” -f ($_.freespace/1gb)}}, @{n='PercentFree';e={“{0:n2}” -f ($_.freespace/$_.size*100)}} `
    | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
    | ConvertTo-HTML -fragment

    # assemble the HTML for our body of the email report.
    $HTMLmessage = @”

    Disk Space Storage Report
    This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.

    body{font: .8em “”Lucida Grande””, Tahoma, Arial, Helvetica, sans-serif;}
    ol{margin:0;padding: 0 1.5em;}
    table{color:#000000;background:#FFFFFF;border-collapse:collapse;width:647px;border:5px solid #900;}
    thead{}
    thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;}
    thead tr{}
    td{padding:.5em 1em;}
    tfoot{}
    tfoot td{padding-bottom:1.5em;}
    tfoot tr{}
    #middle{background-color:#900;}

    $tableFragment

    Generated on $mydate

    “@

    # Set up a regex search and match to look for any tags in our body. These would only be present if the script above found disks below the threshold of free space.
    # We use this regex matching method to determine whether or not we should send the email and report.
    $regexsubject = $HTMLmessage
    $regex = [regex] '(?im)'

    # if there was any row at all, send the email
    if ($regex.IsMatch($regexsubject)) {
    $date = ( get-date ).ToString('yyyy/MM/dd')
    send-mailmessage -from $fromemail -to $users -subject “SQLServerDiskSpace Report -$date” -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server}

    All i want to change in the script is get the report in ascending order for percent free in the email notification

    Can anybody help me

  • #49468

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    Script works perfectly below is the result that i get to my email

    Disk Space Storage Report
    This report was generated because the drive(s) listed below have less than 10 % free space. Drives above this threshold will not be listed.
    __SERVER DriveType VolumeName Name Size (Gb) FreeSpace (Gb) PercentFree
    ABCDSFDCBAD01 3 C: 39.90 3.70 9.26
    ABCDSFDCSDA01 3 C: 39.90 2.65 6.63
    ABCDSFDCSRD02 3 App D: 40.00 1.46 3.65
    ABCDSFDCS1D01 3 OS C: 39.90 3.41 8.54
    ABCDSFDCTFD01 3 OS C: 39.90 3.06 7.67

    I need the list of servers to be in Ascending order with percentfree

  • #49470

    Moderator
    Topics: 2
    Replies: 525
    Points: 24
    Team Member
    Rank: Member

    Hello,

    I would pipe the filtered object through the Sort-Object cmdlet before piping into ConvertTo-HTML.

    | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
    | Sort-Object -Property PercentFree `
    | ConvertTo-HTML -fragment

    Full example:

    I hope that helps and answers your question.

    Best,
    Daniel

  • #49890

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    Thanks Daniel that works perfectly

The topic ‘Powershell script to send an email notification with servers with disk space’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort