Want to send the output of this Powershell script in the body of an email

I've been struggling with trying to figure out how to send the output of this script in the body of an email.

Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | Out-GridView -Title "Drive Space"

I've got the SMTP bits to send

$emailFrom = "admin@mydomain.com"
$emailTo = "me@mydomain.com"
$subject = "Disk Size"
$smtpServer = "mailx.mydomain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body)
$MailMessage.IsBodyHtml = $true
$MailMessage.ReplyTo = "admin@mydomain.com"
$smtp.Send($MailMessage)

Has anyone been able to put the two together?

May 13th, 2010 9:35pm

As far as I know, gridview output is not portable.  

Does this work for you?

 

$body = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | converto-html

$emailFrom = "admin@mydomain.com"
$emailTo = "me@mydomain.com"
$subject = "Disk Size"
$smtpServer = "mailx.mydomain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body)
$MailMessage.IsBodyHtml = $true
$MailMessage.ReplyTo = "admin@mydomain.com"
$smtp.Send($MailMe

Free Windows Admin Tool Kit Click here and download it now
May 13th, 2010 10:06pm

Try this: 

$body = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} |Out-String

Send-MailMessage -To "me@mydomain.com" -From "admin@mydomain.com" -Subject "Disk Size" -SmtpServer "mailx.mydomain.com" -Body $body

Karl

May 13th, 2010 10:35pm

That does get me almost there. It puts the info into an email and sends it. Last night I ran across a post that seemed to give me the best of all worlds

http://blogs.msdn.com/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx

$ErrorActionPreference = "Stop"                       
        try {                       
            $messageParameters = @{                       
                Subject = "Disk Space Summary - $((Get-Date).ToShortDateString())"                       
                Body = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\computers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | Sort-Object "Free Space(%)" |  ConvertTo-Html |                        
                    Out-String                       
                From = "admin@mydomain.com"                       
                To = "sandy.wood@mydomain.com"                       
                SmtpServer = "mailx.mydomain.com"                       
            }                       
            Send-MailMessage @messageParameters -BodyAsHtml                       
        } catch {                       
            $_ |                        
                Out-File $env:TEMP\ProblemsSendingHotfixReport.log.txt -Append -Width 1000                       
        }

I was able to use this to send and format (nicely) my disk information. I still haven't gotten my head around everything yet but it does work! Now if I can just get the Task Scheduling part to work!

Thanks for the help!

Free Windows Admin Tool Kit Click here and download it now
May 14th, 2010 2:16pm

i would like to offer two points:

1 - there is a typo where some of you have used "ConvertTo-Html" - you are missing at "t"

2 - there is a better/simpler cmdlet for sending mail, provided here: 
https://technet.microsoft.com/en-us/magazine/ff642463.aspx

Finally, here's my final script that I was able to build based on your recommendations:

$DiskMail = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\servers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | Out-String

Send-MailMessage -To "email@email.com" -From "email@email.com" -Subject "Disk Report" -SmtpServer "server" -Body $DiskMail
Thanks so much!


  • Edited by David Guirl Thursday, August 27, 2015 1:51 PM
August 27th, 2015 1:50pm

i would like to offer two points:

1 - there is a typo where some of you have used "ConvertTo-Html" - you are missing at "t"

2 - there is a better/simpler cmdlet for sending mail, provided here: 
https://technet.microsoft.com/en-us/magazine/ff642463.aspx

Finally, here's my final script that I was able to build based on your recommendations:

$DiskMail = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content c:\scripts\servers.txt) | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="Free Space(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}},@{Name="Free Space(%)";Expression={"{0:P2}" -f(($_.freespace/1gb) / ($_.size/1gb))}} | Out-String

Send-MailMessage -To "email@email.com" -From "email@email.com" -Subject "Disk Report" -SmtpServer "server" -Body $DiskMail
Thanks so much!


  • Edited by David Guirl Thursday, August 27, 2015 1:51 PM
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 1:50pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics