Link to home
Start Free TrialLog in
Avatar of mobanker
mobankerFlag for United States of America

asked on

Insert a file as a body in a powershell email

I have a script that generates a file which will be saved at c:\myfile.csv. That portion of the script works well.

I would like the PowerShell script to send the file to user@domain.com.

I need to know how to specify the smtp server, the credentials (do I specify name AND password?), and the correct way to specify using the filename as the body of the email message.

Any help would be appreciated. This is my current script;

get-content C:\Serverlist.txt | foreach-object {get-vm -computername $_ } | where {$_.replicationstate -notmatch "Disabled"} | get-VMreplication | select primaryserver, replicaserver, name, replicationmode, State, replicationhealth, @{Expression={"{0:0.0}" -f ($_.FrequencySec / 60)};Label="Target Freq (min)"}, @{Expression={"{0:N0}" -f ((get-date)-($_.lastreplicationtime)).TotalMinutes};Label="Delta (min)"} | Out-File c:\replication.csv

A complete line of script would be most useful!  :)
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mobanker

ASKER

Thank you footech!  I will give this a shot in the morning and post back.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got it to work!  Below is the final output.  I first did it as a text file and emailed it and then decided to format has HTML and now it shows much better in an email.  It helped to know that the credentials would default if not included. I had tried the PS help file and was not getting there, so this was a great help from both of you.

Thank you very much!

get-content C:\list.txt | foreach-object {get-vm -computername $_ } | where {$_.replicationstate -notmatch "Disabled"} | get-VMreplication | select primaryserver, replicaserver, name, replicationmode, State, replicationhealth, @{Expression={"{0:0.0}" -f ($_.FrequencySec / 60)};Label="Target Freq (min)"}, @{Expression={"{0:N0}" -f ((get-date)-($_.lastreplicationtime)).TotalMinutes};Label="Delta (min)"} | ConvertTo-Html | Out-File C:\filename.html
$body= Get-Content C:\filename.html | Out-String
Send-MailMessage -SmtpServer "0.0.0.0" -From "user1@user.com" -To "user2@user.com" -Subject "Report" -BodyAsHtml $body