Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Send email with powershell

Posted on 2013-12-05
17
Medium Priority
?
413 Views
Last Modified: 2013-12-12
I have the following commands that work separately, but I want to results from the first command be sent as email by the second command.

how can I do that?

1***
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} }

2****
Send-MailMessage -From sender@domain.com -To Receiver@domain.com-Subject 'Daily Freespace Report' `-SmtpServer smtpservername


Thanks
0
Comment
Question by:jskfan
  • 7
  • 6
  • 4
17 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 39699975
Sorry, I thought piping the results into Send-MailMessage would work, but it doesn't.
We'll need to create a text file, and send that as attachment:
$file = $env:Temp\DiskReport.txt
gwmi win32_volume -Filter 'drivetype = 3' |
  select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} } |
  ft -auto | Out-File $file

Send-MailMessage -From sender@domain.com -To Receiver@domain.com-Subject 'Daily Freespace Report' `-SmtpServer smtpservername -Attachment $file
Remove-Item $file

Open in new window

0
 
LVL 41

Assisted Solution

by:footech
footech earned 1000 total points
ID: 39701427
Or even without the intermediate file.
$body = gwmi win32_volume -Filter 'drivetype = 3' |
  select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} } |
  ft -auto
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body ($body | out-string)

Open in new window

0
 

Author Comment

by:jskfan
ID: 39701616
let 's start one command at a time:

This does not give any error nor output
$body = gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} }| ft -auto


This will give output, but not converted to Gigabytes:
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} }
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 41

Expert Comment

by:footech
ID: 39701651
It shouldn't output anything to the console as it is stored in the $body variable.
Here's a sample of what the command produces on my computer.
driveletter label    GBfreespace
----------- -----    -----------
            RECOVERY 1.83       
C:          OS       13.42      

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39701661
To see the result, you will have to e.g. just write the var alone:
$body

Open in new window

The command without $body is the same as you used it, and it does return fractional GBs for sure.
0
 

Author Comment

by:jskfan
ID: 39701762
this works now
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} }
0
 

Author Comment

by:jskfan
ID: 39701775
if I copy paste this command to windows powershell and change the send and receiver email and the smtp server

$body = gwmi win32_volume -Filter 'drivetype = 3' |
  select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} } |
  ft -auto
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body ($body | out-string)

it does not give error , but it does not send me email.
0
 
LVL 41

Expert Comment

by:footech
ID: 39701916
When you run the command
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body "testing"

Open in new window

do you get anything?  If not then you need to either adjust the email server config, or possibly adjust the command to work with it's settings.

What are you using for email?  Is it set to allow you to submit email on port 25?  Do you need to provide credentials?
0
 
LVL 41

Expert Comment

by:footech
ID: 39701945
Actually, I expect any issue contacting the SMTP server would generate an error, but I can't be certain.
0
 

Author Comment

by:jskfan
ID: 39702261
yes it sends with this:

Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body "testing"
0
 
LVL 41

Expert Comment

by:footech
ID: 39702334
Well, no reason that it shouldn't work with the other then.  If you didn't get an error and didn't receive the email then I would guess it was filtered out by something in your email system or it was sent to a wrong or invalid email address.
0
 

Author Comment

by:jskfan
ID: 39702582
$body = gwmi win32_volume -computername remotecomputername -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} } |  fl
Send-MailMessage -From me@domain.com.com -To me@shopzilla.com -Subject 'Daily Freespace Report' -SmtpServer smtp.domain.com -Body ($body | out-string)

This is worked ...

======== if you do not mind Guys, I need to  put this in a file and schedule it in the task scheduler.... for now I am just pasting the command into windows powershell and run it...I saved the command into ps1 file but when I run it, it does not do anything other than opening up the ps1 file.
also if you want to add icing on the cake, please change the command so that the email will display the out in form of a table, with output under each header well spaced...

Thank you
0
 

Author Comment

by:jskfan
ID: 39702583
OOps.. just right-clicked on the file and "Run with Powershell" it sent me email...
so it works, but when I schedule it, how am I going to tell it run with Powershell ?
0
 
LVL 41

Expert Comment

by:footech
ID: 39702597
In your last WMI query you piped to "fl" (Format-List).  All other versions piped to "ft" (Format-Table).
Your scheduled task will start powershell.exe, and the argument can be "-file yourscript.ps1".
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39703143
The default action for .ps1 files is Open, which means "Open in an editor", not execute. This is to make it more difficult to run malicious (or unknown) code accidentally.

Hence you have to start PowerShell.exe and provide the script as an argument, as told by footech.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 39703204
As said by me earlier, a HTML table might be better suited for your needs:
$body = gwmi win32_volume -Filter 'drivetype = 3' |
  select driveletter, label, @{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} } |
  ConvertTo-Html
Send-MailMessage -From me@domain.com.com -To me@shopzilla.com -Subject 'Daily Freespace Report' -SmtpServer smtp.domain.com -Body (-join $body) -BodyAsHTML

Open in new window

0
 

Author Closing Comment

by:jskfan
ID: 39715640
Excellent!
Thank you Guys
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question