PowerShell Conditional

How would I use a conditional statement to only send an email when the "test-connection" results in one or all of the servers being down?
$servers = "server1","server2","server3"
$collection = $()
$OutPutFile = "C:\Temp\ConnectivityStatus.csv"
foreach ($server in $servers)
{
    $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
    if (Test-Connection $server -Count 1 -ea 0 -Quiet)
    { 
        $status["Results"] = "Up"
    } 
    else 
    { 
        $status["Results"] = "Down" 
    }
    New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
    $collection += $serverStatus

}
$collection | Export-Csv $OutPutFile -NoTypeInformation



################ EMAIL FILE ###################################################
$From = "email address"
$To = "email address"
$Cc = "email address"
$Subject = ""
$Body = Get-Content C:\Temp\ConnectivityStatus.csv
$SMTPServer = ""
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username" , "password");
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $From
$emailMessage.To.Add($To)
#$emailMessage.Cc.Add($Cc)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\Temp\ConnectivityStatus.csv")
$SMTPClient.Send($emailMessage)
############################################################################

Open in new window

WebccAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Set a variable before checking the servers to an initial value.  Then if the server check fails, change the value.  After checking all the servers, check the variable to see if the value has changed, and if so send the email.
$servers = "server1","server2","server3"
$collection = $()
$OutPutFile = "C:\Temp\ConnectivityStatus.csv"
$sendemail = $false
foreach ($server in $servers)
{
    $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
    if (Test-Connection $server -Count 1 -ea 0 -Quiet)
    { 
        $status["Results"] = "Up"
    } 
    else 
    { 
        $status["Results"] = "Down"
        $sendemail = $true
    }
    New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
    $collection += $serverStatus

}
$collection | Export-Csv $OutPutFile -NoTypeInformation

If ( $sendemail )
{
    ################ EMAIL FILE ###################################################
    $From = "email address"
    $To = "email address"
    $Cc = "email address"
    $Subject = ""
    $Body = Get-Content C:\Temp\ConnectivityStatus.csv
    $SMTPServer = ""
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username" , "password");
    $emailMessage = New-Object System.Net.Mail.MailMessage
    $emailMessage.From = $From
    $emailMessage.To.Add($To)
    #$emailMessage.Cc.Add($Cc)
    $emailMessage.Subject = $Subject
    $emailMessage.Body = $Body
    $emailMessage.Attachments.Add("C:\Temp\ConnectivityStatus.csv")
    $SMTPClient.Send($emailMessage)
    ############################################################################
}

Open in new window

1
aikimarkCommented:
You can break the loop when you encounter the first not up server:
$servers = "server1","server2","server3"
$collection = $()
$OutPutFile = "C:\Temp\ConnectivityStatus.csv"
foreach ($server in $servers)
{
    $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
    if (Test-Connection $server -Count 1 -ea 0 -Quiet)
    { 
        $status["Results"] = "Up"
    } 
    else 
    { 
        $status["Results"] = "Down" 
        break
    }
    New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
    $collection += $serverStatus

}
$collection | Export-Csv $OutPutFile -NoTypeInformation



################ EMAIL FILE ###################################################
if ($status["Results"] -eq "Down" )
{
$From = "email address"
$To = "email address"
$Cc = "email address"
$Subject = ""
$Body = Get-Content C:\Temp\ConnectivityStatus.csv
$SMTPServer = ""
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username" , "password");
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $From
$emailMessage.To.Add($To)
#$emailMessage.Cc.Add($Cc)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\Temp\ConnectivityStatus.csv")
$SMTPClient.Send($emailMessage)
############################################################################
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WebccAuthor Commented:
Thanks guys.
0
footechCommented:
If you use the break, you won't get a report on any of the servers that come after one detected as down.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.