Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Status Monitoring - elseif statements

$
0
0

Hi All,

I have written a script to ping servers every hour and return with results. These in turn can be used to justify the course of action to be taken by the script. It was working at one point, but for some reason, whenever I run the script within the do-until loop it remembers the result from the previous run and will always display the same error over and over, even though the variable that contains the result is deleted at the end of each loop. i.e:

Success Pinging Test1 ...Continuing
Success Pinging TEST2 ...Continuing
Success Pinging Exchange ...Continuing
Success Pinging Exchange MB's ...Continuing
Success Pinging Switch 1 ...Continuing
"Part of Exchange appears to be offline... will write note instead of sending E-mail" ?

Even though the condition's clearly state only if the variable contains the server names. To give clarity, here is my script - don't laugh, its a work in progress ;)

$Servers = Import-CSV "C:\Scripts\Servers.csv"
$TimeStarted = Get-Date
$TimeEnd = $TimeStarted.AddHours(14)

Write-Host "This Script Will Terminate at $TimeEnd"

#Do-Until

Do
{
  $Timenow = Get-Date
  Start-Sleep(3600)
  $failed = @()
  foreach ($Server in $Servers)
    {
     if (Test-Connection $Server.IP -quiet)
        {
         Write-Host "Success Pinging"$Server.Name"...Continuing"
        }
     else
        {
         Write-Host "Ping Test to"$Server.Name"Failed... Adding to Monitor Array"
         $failed += $Server.Name
        }
    }
    $failed
  $Timestamp = (Get-Date).ToShortTimeString()

  if ($failed -contains $null)
    {
     Write-Host "All Servers & Hosts Running!"
    }
  Elseif ($failed -like "Switch1" -and "Switch2")
    { 
     $result = "Switch 1 and`/or Switch 2 was down during this test - Please Check! $TimeStamp `n"
     Write-Host "Switch 1 and`/or Switch 2 was down during this test - Write Note to Check it"
     $result | Out-File "C:\Users\USER\desktop\StatusMonitor.txt" -Append
    }
  Elseif (($failed -like "AD1") -and ($failed -like "AD2") -and ($failed -like "FILESERVER1") -and ($failed -like "FILESERVER2") -and ($failed -like "HOST1") -and ($failed -like "HOST2") -and ($failed -like "SERVER") -and ($failed -like "Exchangemb2") -and ($failed -like "Exchangemb1") -and ($failed -like "Exchange") -and ($failed -like "Server"))
    { 
     $result = "All VM's and Hosts Down! - May have been a power failure... $Timestamp `n"
     Write-Host "All VM's and Hosts Down! - May have been a power failure..."
     $result | Out-File "C:\Users\USER\desktop\StatusMonitor.txt" -Append
    }
  Elseif ($failed -like "Exchange1" -and "Exchangemb1" -and "Exchangemb2")
    {
     $result = "Part of Exchange was offline at $TimeStamp ... `n"
     Write-Host "Part of Exchange appears to be offline... will write note instead of sending E-mail"
     $result | Out-File "C:\Users\USER\desktop\StatusMonitor.txt" -Append
    } 
  Elseif ($failed -like "*")
    {
     $strSmtpServer = "SERVER.DOMAIN.COM"

     $objemail = new-object Net.Mail.MailMessage
     $objSmtpServer = new-object Net.Mail.SmtpClient($strSmtpServer)

     $objemail.From = "SENTFROM@DOMAIN.COM"
     $objemail.To.Add("ADMIN@DOMAIN.COM")
     $objemail.Subject = "Status Monitor"
     $objemail.Body = "Dear Admin,
After running the hourly check it appears the following Servers are/were down:
$failed
Please prepare to investigate, make sure to check status monitor on Users's Laptop located on the desktop to see any other error's that may have occured. This e-mail was checked and compiled at approximatley $TimeStamp"

     $objSmtpServer.send($objemail)
     
        if ($? -eq $False)
        {
         $MessageSendFailure = $objemail.body
         $MessageSendFailure | Out-File "C:\Users\USER\desktop\StatusMonitor.txt" -Append
        }
  Else
   {
    Write-Host "Script Failed"
   }
     }
  Remove-Variable "failed","result"
}
Until ($TimeNow -ge $TimeEnd)
Running this causes it to eventually just loop the same result. no matter what the outcome. I simulate connectivity loss by pulling the Ethernet cable out of my computer.


Bertand1 Answered your Question? - Mark it as the Answer Been helpful rather than hindering? - Mark it as helpful!



Viewing all articles
Browse latest Browse all 2562

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>