locked
Connecting to Exchange shell via new-pssession - selecting a different server if a connection fails RRS feed

  • Question

  • First, I did not see an option under Forum Category specifically for Powershell when I selected 'Ask a question', so if this should be under a different forum, please advise.

    I have a Powershell script that does the following:

    1. Connects to Exchange via new-pssession;

    2. Check the overall health of DB replication and content indexing;

    3. Based on the results of the status and contentindexstate output, sends an email with the results of the output to all Exchange admins with the content in the body of the message as html.

    The following script works as expected:

    $ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri http://cas1.mydomaincom/powershell
    
    if($?){
          "Connected successfully to CAS1"
          }
          else
          {
          "Could not connect to CAS1, trying CAS2"
          $ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri http://cas2.mydomain.com/powershell
          }
    
    import-pssession $ems -DisableNameChecking
    
    $from = "admins@mydomain.com"
    $to = "admins@mydomain.com"
    
    $workfile = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus | sort-Object mailboxserver |select-object -property mailboxserver,name,status,copyqueuelength,replayqueuelength,contentindexstate | convertto-csv -NoTypeInformation
    
    $data = convertfrom-csv -inputobject $workfile | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState"
    
    $status = $data.status
    $index = $data.contentindexstate
    
    function qchk{
    $replFail = if($status -eq "Failed") {
                $results = $data | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState" | ConvertTo-Html
                Send-MailMessage -From $from -To $to -Subject "Exchange Replication Monitoring - Failed" -SmtpServer "cas.mydomain.com" -BodyAsHtml ($results | Out-String)
                } 
    
    $replDiscon = if($status -eq "Disconnected") {
                $results = $data | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState" | ConvertTo-Html
                Send-MailMessage -From $from -To $to -Subject "Exchange Replication Monitoring - Disconnected" -SmtpServer "cas.mydomain.com" -BodyAsHtml ($results | Out-String)
                } 
    
    $replDisconH = if($status -eq "DisconnectedAndHealthy") {
                $results = $data | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState" | ConvertTo-Html
                Send-MailMessage -From $from -To $to -Subject "Exchange Replication Monitoring - DisconnectedAndHealthy" -SmtpServer "cas.mydomain.com" -BodyAsHtml ($results | Out-String)
                }
    
    $replServDown = if($status -eq "ServiceDown") {
                $results = $data | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState" | ConvertTo-Html
                Send-MailMessage -From $from -To $to -Subject "Exchange Replication Monitoring - ServiceDown" -SmtpServer "cas.mydomain.com" -BodyAsHtml ($results | Out-String)
                }
    
    $indexFail = if($index -eq "Failed") {
                $results = $data | select "MailboxServer", "Name", "Status" ,"CopyQueueLength", "ReplayQueueLength", "ContentIndexState" | ConvertTo-Html
                Send-MailMessage -From $from -To $to -Subject "Exchange Content Indexing Monitoring - Failed" -SmtpServer "cas.mydomain.com" -BodyAsHtml ($results | Out-String)
                } 
            }
    
    qchk

    So all is good with the above script, but we have more than 2 Exchange servers, and I'd like to have even more redundancy built in for the script to try to connect a different server if CAS1 and CAS2 are both unavailable.  I have tried different variations of an if/elseif/else statement, and the closest I got was the following:

    if($?)
            {
            "Connected successfully to CAS1"
            }
            elseif($?)
            {
            "Could not connect to CAS1, trying CAS2..."
            $ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri http://cas2.mydomain.com/powershell
            }
            else
            {
            "Could not connect to CAS1 or CAS2, trying CAS3..."
            $ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri http://cas3.mydomain.com/powershell
            }

    With the above, if I put in an incorrect name for CAS1, it goes directly to CAS3.  It doesn't seem to like the middle elseif, maybe it has to do with the $?, but anything else I have tried produces an error.  I have also been looking at the Switch option, but I haven't come up with anything that has worked yet with that option.

    UPDATE:  Here is a sample of the Switch code I am experimenting work (unsuccessfully so far...):

    $cas1 = "http://cas1.mydomain.com/powershell"
    $cas2 = "http://cas2.mydomain.com/powershell"
    $cas3 = "http://cas3.mydomain.com/powershell"
    
    $ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas1
    
    Switch($ems){
                $cas1{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas1; break}
                $cas2{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas2; break}
                $cas3{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas3; break}
        }
    
    $ems = Import-PSSession $ems -DisableNameChecking

    The Switch option is new to me, so I have no doubt I am not using it correctly.

    If anyone has any ideas as to a better way to accomplish this task, it would be greatly appreciated.

    Thanks in advance for your consideration!

    J



    • Edited by winteam900 Friday, January 22, 2016 7:07 PM
    Friday, January 22, 2016 4:40 PM

Answers

  • UPDATE:  The following code SEEMS to work so far:

    $cas1 = "http://cas1.mydomain.com/powershell"
    $cas2 = "http://cas2.mydomain.com/powershell"
    $cas3 = "http://cas3.mydomain.com/powershell"
    
    $ems = $cas1, $cas2, $cas3
    
    Switch($ems){
                $cas1{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas1}
                $cas2{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas2}
                $cas3{$ems = New-PSSession -configurationname microsoft.exchange -ConnectionUri $cas3; break}
        }
    
    $ems = Import-PSSession $ems -DisableNameChecking



    • Marked as answer by winteam900 Friday, January 22, 2016 7:08 PM
    • Unmarked as answer by winteam900 Friday, January 22, 2016 7:25 PM
    • Edited by winteam900 Friday, January 22, 2016 7:47 PM UpdatedCode
    • Marked as answer by winteam900 Friday, January 22, 2016 7:48 PM
    Friday, January 22, 2016 7:07 PM