Link to home
Start Free TrialLog in
Avatar of santoshmotwani
santoshmotwaniFlag for Australia

asked on

powershell help needed

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

Avatar of prashanthd
prashanthd
Flag of India image

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

Avatar of santoshmotwani

ASKER

Nothing comes up in the email
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


still nothing comes up ,,,, can you please write the full code

Thanks
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Spot on !!!