Solved

powershell help needed

Posted on 2011-09-26
6
410 Views
Last Modified: 2012-06-21
Hi Experts,

I have attached a code .

This code checks for the latest log file and emails it to me as an attachment.

I would like to change this script so that instead of attaching log file it should email me the contents . I receive 100's error every day so its hard for to open each and every log file.

Any help will be appreciated.

Thanks
#E#Email Function
Function Email {
Param($Attach)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $Body)
Foreach ($msgAttach in $Attach) {
$Attachment = new-object Net.Mail.Attachment($msgAttach.Fullname)
$MailMessage.Attachments.Add($Attachment)
}
$smtp.Send($MailMessage)
}

#File Params
$LogFile = "D:\log.txt"
$MonitorDirectory = "\\logserver\logs"
#Email Params
$EmailFrom = "my@my.com"
$EmailTo = "my@my.com"
$subject = "New Error Files detected"
$body = "Attached are new Error files which have been detected "
$smtpServer = "xxxx"

#Script begins
$SentFiles = Get-Content $LogFile
$NewFiles = Get-ChildItem $MonitorDirectory | Where {$SentFiles -NotContains $_.FullName}
if($NewFiles) {
Foreach ($NewFile in $NewFiles) {
	$FileName = $NewFile.FullName
	Email $NewFile
	Add-Content $LogFile $FileName
}
}

Open in new window

0
Comment
Question by:santoshmotwani
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 36708663
Try the following..
#E#Email Function
Function Email {
Param($body)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $Body)
#Foreach ($msgAttach in $Attach) {
#$Attachment = new-object Net.Mail.Attachment($msgAttach.Fullname)
#$MailMessage.Attachments.Add($Attachment)
#}
$smtp.Send($MailMessage)
}

#File Params
$LogFile = "D:\log.txt"
$MonitorDirectory = "\\logserver\logs"
#Email Params
$EmailFrom = "my@my.com"
$EmailTo = "my@my.com"
$subject = "New Error Files detected"
#$body = "Attached are new Error files which have been detected "
$smtpServer = "xxxx"

#Script begins
$SentFiles = Get-Content $LogFile
$NewFiles = Get-ChildItem $MonitorDirectory | Where {$SentFiles -NotContains $_.FullName}
if($NewFiles) {
Foreach ($NewFile in $NewFiles) {
	$FileName = $NewFile.FullName
	$body=Get-Content $MonitorDirectory+"\"+$NewFile.name
	Email $body
	Add-Content $LogFile $FileName
}
}

Open in new window

0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 36713808
Nothing comes up in the email
0
 
LVL 1

Expert Comment

by:jjthomas3
ID: 36713954
I too got nothing until I changed the foreach loop a little:
 
Foreach ($NewFile in $NewFiles) {
	$FileName = $NewFile.FullName
	[string]$content = (Get-Content -Path $FileName -Force)
	Email $content
	Add-Content $LogFile $FileName
}

Open in new window


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Author Comment

by:santoshmotwani
ID: 36714085
still nothing comes up ,,,, can you please write the full code

Thanks
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 36716062
Try the following..
#Email Function
Function Email {
Param([string]$Body)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $Body)
$smtp.Send($MailMessage)
}

#File Params
$LogFile = "D:\log.txt"
$MonitorDirectory = "\\logserver\logs"
#Email Params
$EmailFrom = "my@my.com"
$EmailTo = "my@my.com"
$subject = "New Error Files detected"
#$body = "Attached are new Error files which have been detected "
$smtpServer = "xxxx"

#Script begins
$SentFiles = Get-Content $LogFile
$NewFiles = Get-ChildItem $MonitorDirectory | Where {$SentFiles -NotContains $_.FullName}
if($NewFiles) {
Foreach ($NewFile in $NewFiles) {
	$FileName = $NewFile.FullName
	$body=Get-Content "$MonitorDirectory\$NewFile"
	Email $body
	Add-Content $LogFile $FileName
}
}

Open in new window

0
 
LVL 16

Author Closing Comment

by:santoshmotwani
ID: 36720778
Spot on !!!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now