此脚本在触发特定事件时发送电子邮件。当我在 FSRM 上配置的文件筛选器检测到用户保存特定文件类型时,我将运行此脚本。命令选项卡是我运行 powershell.exe 的地方,也是此脚本的参数。我试图从文件筛选器配置为生成的事件日志中添加消息。但是,当此脚本生成电子邮件时,它会生成以下内容而不是事件消息。
System.Diagnostics.EventLogEntry
脚本如下:
function
sendMail{
Write-Host “Sending Email”
#SMTP server name
$smtpServer = “smtp.abc.com”
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#gets the server name
$srv = $env:computername
#gets the event ID details
$event = Get-Eventlog -LogName Application -source SRMSVC -Newest 1
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = “[email protected]“
$msg.ReplyTo = “[email protected]“
$msg.To.Add(“[email protected]“)
$msg.subject = “Event Alert”
$msg.body = “The file resource management service has detected activity. Please check the appliction log on $srv and look for id 8215. Here are the event details $event ”
#Sending email
$smtp.Send($msg)
}
#Calling function
sendMail
通过在电子邮件正文中添加正确的计算机名称,$srv 变量即可正确输入。
有人知道为什么事件消息没有包含在电子邮件正文中吗?
答案1
尝试改变
$msg.body = “The file resource management service has detected activity. Please check the appliction log on $srv and look for id 8215. Here are the event details $event ”
到
$msg.body = “The file resource management service has detected activity. Please check the appliction log on $srv and look for id 8215. Here are the event details $($event.message) ”
我所做的就是 $event
改变 $($event.message)