Email Powershell在服务关闭时发送电子邮件

Email Powershell在服务关闭时发送电子邮件,email,powershell,service,Email,Powershell,Service,我制作了两个脚本,一个检查文件,一个检查服务。 他们都是分开工作的,当我试图将他们结合起来时,我没有收到电子邮件。没有问题或错误,运行正常,但没有电子邮件 $Path = "D:\temp\" $Text = "[ERROR] - Unable to connect to target queue." $PathArray = @() $Servername = $env:computername $service = Get-WmiObject win32_service -computern

我制作了两个脚本,一个检查文件,一个检查服务。 他们都是分开工作的,当我试图将他们结合起来时,我没有收到电子邮件。没有问题或错误,运行正常,但没有电子邮件

$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
                    }

                }

您正在使用两种完全不同的方法发送邮件。问题不是将这两部分结合起来,而是如何调用
Send MailMessage
,实际上我必须使用if条件,我不知道如何使用smpt。Send在其他服务器上不起作用。你能告诉我当我使用smtp.send时,第一个邮件的问题是什么吗?你可以在
If
语句中使用
smtp.send
,如果有效,为什么不继续使用它呢?你使用的是两种完全不同的方法来发送邮件。问题不是将这两部分结合起来,而是如何调用
Send MailMessage
,实际上我必须使用if条件,我不知道如何使用smpt。Send在其他服务器上不起作用。你能告诉我当我使用smtp.send时,第一个它工作时出现了什么问题吗?你可以在
If
语句中使用
smtp.send
,如果它工作,为什么不继续使用它呢?
$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)