locked
How to email free space report for Mount points RRS feed

  • Question

  • Let me preface this post. I am new to powershell. I have Windows server 2008 R2 with logical disks and 20 mounted volumes from our SAN under one of the logical disks. I need a simple report to run twice a day and I can get the powershell command to display on the screen but porting it and emailing to me is making me cross-eyed. Can someone take a look at my script? It should be pretty simple...

    # Start of Script......

    $messageParameters = @{

    # Date-Time
    $a = Get-Date -format g

    # begin the message
     Subject = "Server01 Space Report $a"

    #creates the expression to get the mount points for the the server list

    $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
     $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
     $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
     
     Body = function get-mountpoints {
       $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}
       $volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
       }
    # get a list of servers from external source
    $servers = (Get-Content .\servers.txt)
     
    foreach ($server in $servers){
     get-mountpoints
    }

    #convert the screen display to HTML
    ConvertTo-Html |

     From = "Myemail@mailserver.com"
     To = "Myemail@mailserver.com"
     SmtpServer = "mailrelay.server.local"
     }
    Send-MailMessage @messageParameters -BodyAsHtml
    }

    Thank you in advance

    Tom G

    Tuesday, February 14, 2012 7:40 PM

Answers

  • I hope this does what you need.

    function get-mountpoints {

    param( $server )
    $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
    $volumes = Get-WmiObject -ComputerName $server -Class win32_volume | Where-object {$_.DriveLetter -ne $null}
    $volumes | select systemname, name, label, $TotalGB, $FreeGB, $FreePerc | ConvertTo-Html | Out-String  
    }
    $messageParameters = @{
    Subject = "Space Report " + (Get-Date -Format g)
    body = (Get-Content servers.txt | ForEach-Object {
    get-mountpoints $_
    } ) | out-string
    From = "sendername@mycompany.com" 
    To = "receivername@mycompany.com"
    SmtpServer = "smtp.mycompany.local" 
    BodyAsHtml = $true
    }
    Send-MailMessage @messageParameters

    • Marked as answer by tg2143 Wednesday, February 15, 2012 2:19 PM
    Wednesday, February 15, 2012 10:27 AM

All replies

  • I believe that you are able to build text you want. Just populate below variables and use following code. It should work. Let me know if you get any error.

     $emailFrom = "Fromemailid@example.com"
     $emailTo = "Toemailid@example.com"
     $subject = $SubjectLine
     $body = $BodyText
     $smtpServer = "YourSMTPServer"
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)
     $smtp.Send($emailFrom, $emailTo, $subject, $body)
    NJoy scripting... :D
    • Proposed as answer by Yan Li_ Wednesday, February 15, 2012 2:48 AM
    Tuesday, February 14, 2012 10:00 PM
  • I hope this does what you need.

    function get-mountpoints {

    param( $server )
    $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
    $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
    $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
    $volumes = Get-WmiObject -ComputerName $server -Class win32_volume | Where-object {$_.DriveLetter -ne $null}
    $volumes | select systemname, name, label, $TotalGB, $FreeGB, $FreePerc | ConvertTo-Html | Out-String  
    }
    $messageParameters = @{
    Subject = "Space Report " + (Get-Date -Format g)
    body = (Get-Content servers.txt | ForEach-Object {
    get-mountpoints $_
    } ) | out-string
    From = "sendername@mycompany.com" 
    To = "receivername@mycompany.com"
    SmtpServer = "smtp.mycompany.local" 
    BodyAsHtml = $true
    }
    Send-MailMessage @messageParameters

    • Marked as answer by tg2143 Wednesday, February 15, 2012 2:19 PM
    Wednesday, February 15, 2012 10:27 AM
  • I appreciate the reply. But some of your syntax did not work when I tried to plug in my code. It was probably a failure on my  part as a noob to powershell. No fault of yours and I'm sure this would have worked if I had more time to explore. 

    Thank you, Tom G

    Wednesday, February 15, 2012 2:17 PM
  • This seems to work except there was a error when you copied the my code  {$_.DriveLetter -eq $null}. I fixed the issue and created the task to run. Thanks again for the help!

    Thank you, Tom G

    Wednesday, February 15, 2012 2:19 PM