santoshmotwani
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
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
}
}
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
}
ASKER
still nothing comes up ,,,, can you please write the full code
Thanks
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Spot on !!!
Open in new window