Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell help needed

Posted on 2011-09-26
6
Medium Priority
?
436 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 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