Email log file after script completes - Powershell

Hey Experts.  I'd like to add the ability to email the log file once the script has finished running.  Below is the script I'd like to add the email functionality to:

$ServerList = @'
	"Name",			"Path"
	Servers
'@ | ConvertFrom-Csv
$LogFile = "\\w2k12FS01\PUBLIC\Tivoli_Master_Logs\Combined_{0}.log"

$Yesterday = (Get-Date).AddDays(-1)
$Pattern = (Get-Date).AddDays(-1).ToString('MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$Timestamp = $Yesterday.ToString('yyyyMMdd')
$in = $false
$ServerList | ForEach-Object {
	$Server = $_
	"========== $($Server.Name) ============================================================"
	Try {
		Switch -Wildcard (Get-Content -Path "\\$($Server.Name)\$($Server.Path.Replace(':', '$'))" -ErrorAction Stop) {
			$Pattern					{$in = $true}
			'*SCHEDULEREC STATUS END*'	{$in = $false}
			default						{If ($in) {$_}}
		}
	} Catch {
		"ERROR accessing '$($Server.Path)': $($_.Exception.Message)"
	}
} | Set-Content -Path $($LogFile -f $TimeStamp)

Open in new window


I tried adding this at the end of the script but it didn't work because of an error (Tivoli_Master_Logs\Combined_{0}.log value cannot be null).

$EmailFrom = "me"
$EmailTo = "me"
$EmailSubject = "Backup results for $($yesterday)"  
$SMTPServer = "smtp"

$emailattachment = $LogFile

$mailmessage = New-Object system.net.mail.mailmessage 
$mailmessage.from = ($emailfrom) 
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
  $mailmessage.Attachments.Add($attachment)
$mailmessage.Body = ("<br />$('*'*72)<br />" + ((Get-Content -Path $LogFile) -join '<br />') + "<br />$('*'*72)<br />")

$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)  
#$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword") 
$SMTPClient.Send($mailmessage)

Open in new window


Thanks experts for your help!
LVL 9
samiam41Asked:
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.

oBdACommented:
Try it like this:
$ServerList = @'
	"Name",			"Path"
	"MGT01",		"E:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
	"w2k8sentry01",	"C:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
	"w2k12share01",	"C:\Program Files\Tivoli\TSM\baclient\dsmsched.log"
'@ | ConvertFrom-Csv
$LogFile = "\\w2k12FS01\PUBLIC\Tivoli_Master_Logs\Combined_{0}.log"

$Yesterday = (Get-Date).AddDays(-1)
$Pattern = $Yesterday.ToString('MM\/dd\/yyyy') + ' * --- SCHEDULEREC STATUS BEGIN*'
$Timestamp = $Yesterday.ToString('yyyyMMdd')
$in = $false
$ServerList | ForEach-Object {
	$Server = $_
	"========== $($Server.Name) ============================================================"
	Try {
		Switch -Wildcard (Get-Content -Path "\\$($Server.Name)\$($Server.Path.Replace(':', '$'))" -ErrorAction Stop) {
			$Pattern					{$in = $true}
			'*SCHEDULEREC STATUS END*'	{$in = $false}
			default						{If ($in) {$_}}
		}
	} Catch {
		"ERROR accessing '$($Server.Path)': $($_.Exception.Message)"
	}
} | Set-Content -Path $($LogFile -f $TimeStamp)

$EmailFrom = "me"
$EmailTo = "me"
$EmailSubject = "Backup results for $($Yesterday)"  
$SMTPServer = "smtp"
$EmailAttachment = $LogFile
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPServer -Attachments $EmailAttachment -Body $EmailSubject

Open in new window

1
samiam41Author Commented:
Here's what I get:

oBdA_email_error.png
0
oBdACommented:
Sorry, too much copy and paste.
Replace line 31 with this:
$EmailAttachment = $LogFile -f $TimeStamp

Open in new window

2

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
samiam41Author Commented:
Script is really looking slick and very functional.  Thanks for your hard-work!
0
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.