Powershell在服务中断时发送电子邮件

时间:2016-09-02 21:13:06

标签: email powershell service

我制作了两个用于检查文件的脚本和一个用于检查服务的脚本。 当我试图将它们组合起来时,它们都是分开工作我没有收到电子邮件。它没有任何问题或错误,但没有电子邮件。

$Path = "D:\temp\"
$Text = "[ERROR] - Unable to connect to target queue."
$PathArray = @()
$Servername = $env:computername
$service = Get-WmiObject win32_service -computername $env:computername | select name,state | where { $_.name -like "iTrax*"} | 
Where {$_.state -eq "Stopped"} | Where {$_.ExitCode -eq "0" } | out-string
Get-ChildItem $Path -Filter "listener*-business-output*.log" |
Where-Object { $_.Attributes -ne "Directory"} |
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
$PathArray += $_.FullName
}
}

Write-Host "Contents of ArrayPath:"
 foreach ($File in $Filelist) {
    If (([DateTime]::UtcNow - $File.LastWriteTimeUtc).totalminutes -ge 30)
        {$FilenameList += $File.Name;
        }
    }
$name = Get-WmiObject win32_service -computername $env:computername | where { $_.name -like "iTrax*"} | 
Where {$_.state -eq "Stopped"} | Where {$_.ExitCode -eq "0" } | out-string
$FilenameList = @();
$filelist = Get-ChildItem -Path "D:\temp" -Filter Listener*-business-output.log
$PathArray | ForEach-Object {$_}
$Servername = $env:computername
function FuncCheckService{
param($ServiceName)
$arrService = Get-Service -Name "iTrax*"
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
                    Send-MailMessage -from 'rahul.lalwani@xpo.com' -to 'Trax <SCscittraxsupport@xpo.com>', 
                    -subject 'ALERT!! Listener down on $servername.' 
                    -Encoding Unicode -body 'Listener Down $Service' -Priority High -port 25 -smtpserver mailhost.menlolog.com  
                    }

                    Elseif ($arrservice.Status -eq "Running") {out-string
                    Send-MailMessage -from 'rahul.lalwani@xpo.com' -to 'TRAX <SCscittraxsupport@xpo.com>'  -subject 'All services are up and running' -body 'Perfect! All Is Well!' -Encoding Unicode -port 25 -smtpserver mailhost.cnf.com
                    }

                }

而只有服务检查的那个没有任何问题。

$service = Get-WmiObject win32_service -computername $env:computername | select name,state | where { $_.name -like "iTrax*"} | Where {$_.state -eq "Stopped"} | Where {$_.ExitCode -eq 0 } | out-string
        # Specify a sender email address
        $Servername = $env:computername
        $emailFrom = "scittraxsupport@xpo.com"
        # Specify a recipient email address
        $emailto = "Rahul.lalwani@xpo.com"
        # Put in a subject line
        $subject = "Listener down on $Servername"
       # Add the Service state from line 6 to some body text
       $body =  "Service $service on"
       # Put the DNS name or IP address of your SMTP Server
       $smtpServer = "mailhost.menlolog.com"
       $smtp = new-object Net.Mail.SmtpClient($smtpServer)
       # This line pieces together all the info into an email and sends it
       $smtp.Send($emailFrom, $emailTo, $subject, $body)

0 个答案:

没有答案