• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 53
  • Last Modified:

Include log file in body of script - Powershell

Hey Experts!  Here's a script that will email a log file.  I'd like to include the log file into the body of the email as well as keep the log file as an attachment.

$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)

$EmailFrom = "me"
$EmailTo = "me"
$EmailSubject = "Backup results for $($yesterday)"  
$SMTPServer = "smtp"
$EmailAttachment = $LogFile -f $TimeStamp

Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $EmailSubject -SmtpServer $SMTPServer -Attachments $EmailAttachment -Body $EmailSubject

Open in new window


I have this code from another script I have that works and am trying to use it but can't get it to work with this script.  Any help would be appreciated!

$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

0
samiam41
Asked:
samiam41
  • 2
  • 2
1 Solution
 
oBdACommented:
Try this then:
$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"

$LogFileTimeStamped = $LogFile -f $TimeStamp
$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 $LogFileTimeStamped

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

Open in new window

0
 
samiam41Author Commented:
-Path $LogFileTimeStamped is invalid/null

Should that be $LogFile $Timestamp with a . or something else in-between?
0
 
oBdACommented:
No, that just ended up in the wrong line, sorry.
$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')
$LogFileTimeStamped = $LogFile -f $TimeStamp
$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 $LogFileTimeStamped

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

Open in new window

1
 
samiam41Author Commented:
Party like a rock star!  Thanks oBdA!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now