Solved

powershell help needed

Posted on 2011-09-26
6
415 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

774 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