Solved

powershell help needed

Posted on 2011-09-26
6
402 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
Comment Utility
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
Comment Utility
Nothing comes up in the email
0
 
LVL 1

Expert Comment

by:jjthomas3
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Author Comment

by:santoshmotwani
Comment Utility
still nothing comes up ,,,, can you please write the full code

Thanks
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
Comment Utility
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
Comment Utility
Spot on !!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Learn about cloud computing and its benefits for small business owners.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

17 Experts available now in Live!

Get 1:1 Help Now