电子邮件通知应从提到的服务器触发

时间:2013-03-15 11:46:10

标签: powershell

我创建了一个函数,在一个公共脚本中,这个公共脚本被服务器号使用,我想添加主机名,以便电子邮件通知应该适用于特定服务器。

与ser1 / ser2 / ser3 / ser4一样 - common.ps1

电子邮件通知仅适用于ser2。

我希望电子邮件通知只能从ser2触发而不能从其他服务器触发。

如何在下面的脚本中添加hostname / servername。

function SendMail { 
$date_DM=Get-date
Write-Host "Sending Email"
$smtpServer = "$MAIL_SERVER"

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($UPDATE_LOG_FILE)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "$MAIL_SENDER"
$msg.To.Add("$MAIL_RECIPIENT")
$msg.Subject = "Script Failed With 1"
$msg.Body = "script is failed Today $date_DM, Please have a look into Log Files \\$HOSTNAME_PROD\data$\script_logs"
$msg.Attachments.Add($att)

$smtp.Send($msg)
$att.Dispose()

}

======================================

调用脚本 -

  in log file if I get  dc=1 (other code)
    SendMail

任何帮助。谢谢。

此致 纳温

1 个答案:

答案 0 :(得分:1)

这样的东西?

function SendMail { 
if ([environment]::MachineName -ne 'ser2'){return}

$date_DM=Get-date
Write-Host "Sending Email"
$smtpServer = "$MAIL_SERVER"

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($UPDATE_LOG_FILE)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = "$MAIL_SENDER"
$msg.To.Add("$MAIL_RECIPIENT")
$msg.Subject = "Script Failed on With 1"
$msg.Body = "script is failed Today $date_DM, Please have a look into Log Files \\$HOSTNAME_PROD\data$\script_logs"
$msg.Attachments.Add($att)

$smtp.Send($msg)
$att.Dispose()

}