jskfan
asked on
Send email with powershell
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';EXPR ESSION={"{ 0:N2}" -f ($_.freespace/1GB)} }
2****
Send-MailMessage -From sender@domain.com -To Receiver@domain.com-Subjec t 'Daily Freespace Report' `-SmtpServer smtpservername
Thanks
how can I do that?
1***
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR
2****
Send-MailMessage -From sender@domain.com -To Receiver@domain.com-Subjec
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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';EXPR ESSION={"{ 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';EXPR ESSION={"{ 0:N2}" -f ($_.freespace/1GB)} }
This does not give any error nor output
$body = gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR
This will give output, but not converted to Gigabytes:
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR
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.
Here's a sample of what the command produces on my computer.
driveletter label GBfreespace
----------- ----- -----------
RECOVERY 1.83
C: OS 13.42
To see the result, you will have to e.g. just write the var alone:
$body
The command without $body is the same as you used it, and it does return fractional GBs for sure.
ASKER
this works now
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR ESSION={"{ 0:N2}" -f ($_.freespace/1GB)} }
gwmi win32_volume -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR
ASKER
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';EXPR ESSION={"{ 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.
$body = gwmi win32_volume -Filter 'drivetype = 3' |
select driveletter, label, @{LABEL='GBfreespace';EXPR
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.
When you run the command
What are you using for email? Is it set to allow you to submit email on port 25? Do you need to provide credentials?
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body "testing"
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?
Actually, I expect any issue contacting the SMTP server would generate an error, but I can't be certain.
ASKER
yes it sends with this:
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body "testing"
Send-MailMessage -From sender@domain.com -To Receiver@domain.com -Subject 'Daily Freespace Report' -SmtpServer smtpservername -Body "testing"
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.
ASKER
$body = gwmi win32_volume -computername remotecomputername -Filter 'drivetype = 3' |select driveletter, label, @{LABEL='GBfreespace';EXPR ESSION={"{ 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
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
ASKER
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 ?
so it works, but when I schedule it, how am I going to tell it run with Powershell ?
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".
Your scheduled task will start powershell.exe, and the argument can be "-file yourscript.ps1".
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.
Hence you have to start PowerShell.exe and provide the script as an argument, as told by footech.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent!
Thank you Guys
Thank you Guys
We'll need to create a text file, and send that as attachment:
Open in new window