?
Solved

power shell to read a txt file into msg body and send email

Posted on 2010-01-08
5
Medium Priority
?
990 Views
Last Modified: 2012-05-08
Hi ,
I need a power script to read a txt file and put the content of the txt file into message boy then email to me.

Please help!

Thanks
0
Comment
Question by:changjia
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:evetsleep
ID: 26212424
Here is a basic way to do this.  Usage is .
\sendFile.ps1 -file test.txt.
param([string]$file)

[string]$fileData = Get-Content $file

if(!$fileData)
	{
	Write-Host "Nothing in File."
	break
	}

$msg = new-object system.net.mail.MailMessage
$SMTPClient = new-object system.net.mail.smtpClient
$SMTPServer = "smtpserver.domain.com"
$SMTPClient.host = $SMTPServer
$msg.From = "fromAddress@domain.com"
$msg.To.Add("toAddress@domain.com")
$msg.Subject = "Contents of $file"
$msg.Body = $fileData
$SMTPClient.Send($Msg)

Open in new window

0
 

Author Comment

by:changjia
ID: 26212712
Hi evetsleep:

Thanks for the code. Could you explain to me what the param on line one does?
Also, what does [string]$ file do?

Thanks
0
 
LVL 1

Accepted Solution

by:
evetsleep earned 2000 total points
ID: 26213384
param is used to provide a parameter for the script.

param([string]$file) allows for sendFile.ps1 -file  where -file is $file in param.  [string] basically tells PowerShell that the value entered after -file will be a string.  So if you were to do this:

.\sendfile.ps1 -file messageBody.txt

the string messgeBody.txt is stored in $file.  We then use Get-Content to .. well get the content of that file and store it in $fileData.

You 'could' hard code the file name if you wanted, and not use param, but I find it more useful to allow for parameters
0
 

Author Comment

by:changjia
ID: 26273178
Got it. Thanks alot!
So, I see there is a 'if' statement to check if the file is empty, don't send.
My file has a header, so the first line always going to be in the file. How to check the file start from the second line?

Thanks
0
 
LVL 1

Expert Comment

by:evetsleep
ID: 26275356
could you give an example of what the file would look like?
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
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 anti-spam), the admin…

850 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