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!  :)
mobankerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
A quick note about your existing code - it's less confusing if you use file extensions that are inline with what the file contains.  In this case, replication.csv isn't actually a CSV file - but it may be a good idea to change that by using Export-Csv instead of Out-File.  And when you're reading in a file, it makes a difference what type of file you're reading (i.e. simple text or .CSV), since you might use Get-Content or Import-CSV.

It's very easy to send email using the Send-MailMessage cmdlet, read the help on it.  There's a parameter for the SMTP server.  What settings you use (e.g. for credentials, port, SSL) will depend on the configuration of your SMTP server.  Any time you supply credentials you'll supply a password - whether directly in the code or at a prompt at runtime depends on your code.

As for the body, there's decisions to be made about formatting.  Plain-text or HTML?  How you handle each will depend on what the body contents are - e.g. if they are in table format or not.  If plain-text, table columns will not line up if the email client isn't using a fixed-width font for displaying text emails.
Here's a simple example of reading a file into the -body parameter of Send-MailMessage
-body (Get-Content somefile.txt | Out-String)
So you could have a complete command like
Send-MailMessage -From "me@domain.com" -To "user@domain.com" -Subject "read this" -Body (Get-Content somefile.txt | Out-String) -SmtpServer "smtp.domain.com"

Open in new window

mobankerAuthor Commented:
Thank you footech!  I will give this a shot in the morning and post back.
Christopher Jay WolffWiggle My Legs, OwnerCommented:
Hi.
Yes on that.

As another example, you most likely will need the details through parameters.

$c=Get-Credential
$body= Get-Content C:\users\local\documents\bologna1.txt | Out-String
Send-MailMessage -SmtpServer smtp-mail.outlook.com -port 587 -credential $c -from "Chris@hotmail.com" -to "Chris@yahoo.com" -subject "Testing a script." -body $body -UseSSL

Open in new window


And here is the bit on credentials for Send-MailMessage.

-Credential <PSCredential>
     Specifies a user account that has permission to perform this action. The default is the current user.

     Type a user name, such as "User01" or "Domain01\User01". Or, enter a PSCredential object, such as one from the
     Get-Credential cmdlet.

     Required?                    false
     Position?                    named
     Default value                Current user
     Accept pipeline input?       false
     Accept wildcard characters?  false

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mobankerAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.