Powershell脚本发送3封电子邮件而不是1封

时间:2015-05-07 18:36:38

标签: email powershell

我有一个ping ip地址的脚本,如果一个或多个不成功,就会发送一封电子邮件。该脚本正在运行,但是当它发送电子邮件时,它会发送3份而不是仅发送一份。任何帮助,将不胜感激。

param($alert)

# Create the variables
$global:GArgs = $Args
$TimeStop = "23:55"
$errorCount = 0

$urlsToTest = @{}
$urlsToTest["HCC Brandon"] = "ip address"
$urlsToTest["HCC Dale Mabry"] = "ip address"
$urlsToTest["HCC Plant City"] = "ip address"
$urlsToTest["HCC South Shore"] = "ip address"
$urlsToTest["HCC Ybor"] = "ip address"
$urlsToTest["HCC Admin"] = "ip address"

$errorTime =@{}
$errorTime["HCC Brandon"] = "5/7/2015 02:00:00 AM"
$errorTime["HCC Dale Mabry"] = "5/7/2015 02:00:00 AM"
$errorTime["HCC Plant City"] = "5/7/2015 02:00:00 AM"
$errorTime["HCC South Shore"] = "5/7/2015 02:00:00 AM"
$errorTime["HCC Ybor"] = "5/7/2015 02:00:00 AM"
$errorTime["HCC Admin"] = "5/7/2015 02:00:00 AM"

$ping = new-object System.Net.NetworkInformation.Ping
Do {
foreach ($key in $urlsToTest.Keys) {
   $output = ""
   $startTime = get-date
   $output = $ping.send($urlsToTest[$key])
   $endTime = get-date

   if (((New-TimeSpan -Start $errorTime[$key]).hours) -ge "1") {
   if ($output.status.tostring() –eq "Success") {
      $key + "`t`tSuccess`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"

      if ($GArgs -eq "-log") {
         $key + "`t`tSuccess`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> BACboneSiteTest.log
      }
   } else {
      $errorCount++
      $key + "`t`tFail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds"
      $errorTime[$key] = Get-Date

      if ($GArgs -eq "-log") {
         $key + "`t`tFail`t`t" + $startTime.DateTime + "`t`t" + ($endTime - $startTime).TotalSeconds + " seconds" >> BACboneSiteTest.log
      }
   }
   } else {
   $key + "`t`tSkipping`t`t"
   }
   }
   if ($errorCount -ge 1) {
   if ($alert) {
         $EmailFrom = "emailfrom"
         $EmailTo = "emailto"
         $Subject = "BACbone Test Failure - " + $startTime 
         $body = "BACbone Test Failure: " + $startTime
         $smtpServer = "smtp.gmail.com"
         $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
         $SMTPClient.EnableSsl = $true 
         $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
         $SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$body)      
      }
      }
$errorCount = 0
start-sleep -seconds 30
}Until ((get-date).Hour -eq $TimeStop.Hour -and (get.date).Minute -eq $TimeStop.Minute)
$ping = $null

0 个答案:

没有答案