{"id":269,"date":"2019-07-16T10:51:19","date_gmt":"2019-07-16T14:51:19","guid":{"rendered":"http:\/\/www.thelamberts.com\/wiki\/?p=269"},"modified":"2019-10-11T15:55:08","modified_gmt":"2019-10-11T19:55:08","slug":"paul-cunninghams-test-exchangeserverhealth-ps1","status":"publish","type":"post","link":"http:\/\/www.thelamberts.com\/wiki\/2019\/07\/16\/paul-cunninghams-test-exchangeserverhealth-ps1\/","title":{"rendered":"Paul Cunningham&#8217;s Test-ExchangeServerHealth.ps1"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;#\n.SYNOPSIS\nTest-ExchangeServerHealth.ps1 - Exchange Server Health Check Script.\n\n.DESCRIPTION \nPerforms a series of health checks on Exchange servers and DAGs\nand outputs the results to screen, and optionally to log file, HTML report,\nand HTML email.\n\nUse the ignorelist.txt file to specify any servers, DAGs, or databases you\nwant the script to ignore (eg test\/dev servers).\n\n.OUTPUTS\nResults are output to screen, as well as optional log file, HTML report, and HTML email\n\n.PARAMETER Server\nPerform a health check of a single server\n\n.PARAMETER ReportMode\nSet to $true to generate a HTML report. A default file name is used if none is specified.\n\n.PARAMETER ReportFile\nAllows you to specify a different HTML report file name than the default.\n\n.PARAMETER SendEmail\nSends the HTML report via email using the SMTP configuration within the script.\n\n.PARAMETER AlertsOnly\nOnly sends the email report if at least one error or warning was detected.\n\n.PARAMETER Log\nWrites a log file to help with troubleshooting.\n\n.EXAMPLE\n.\\Test-ExchangeServerHealth.ps1\nChecks all servers in the organization and outputs the results to the shell window.\n\n.EXAMPLE\n.\\Test-ExchangeServerHealth.ps1 -Server HO-EX2010-MB1\nChecks the server HO-EX2010-MB1 and outputs the results to the shell window.\n\n.EXAMPLE\n.\\Test-ExchangeServerHealth.ps1 -ReportMode -SendEmail\nChecks all servers in the organization, outputs the results to the shell window, a HTML report, and\nemails the HTML report to the address configured in the script.\n\n.LINK\nhttps:\/\/practical365.com\/exchange-server\/powershell-script-exchange-server-health-check-report\/\n\n.NOTES\nWritten by: Paul Cunningham\n\nFind me on:\n\n* My Blog:\thttp:\/\/paulcunningham.me\n* Twitter:\thttps:\/\/twitter.com\/paulcunningham\n* LinkedIn:\thttp:\/\/au.linkedin.com\/in\/cunninghamp\/\n* Github:\thttps:\/\/github.com\/cunninghamp\n\nFor more Exchange Server tips, tricks and news\ncheck out Exchange Server Pro.\n\n* Website:\thttps:\/\/practical365.com\n* Twitter:\thttps:\/\/twitter.com\/practical365\n\nAdditional Credits (code contributions and testing):\n- Chris Brown, http:\/\/twitter.com\/chrisbrownie\n- Ingmar Br\u00fcckner\n- John A. Eppright\n- Jonas Borelius\n- Thomas Helmdach\n- Bruce McKay\n- Tony Holdgate\n- Ryan\n- Rob Silver\n- andrewcr7, https:\/\/github.com\/andrewcr7\n\nLicense:\n\nThe MIT License (MIT)\n\nCopyright (c) 2017 Paul Cunningham\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and\/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\nChange Log\nV1.00, 05\/07\/2012 - Initial version\nV1.01, 05\/08\/2012 - Minor bug fixes and removed Edge Tranport checks\nV1.02, 05\/05\/2013 - A lot of bug fixes, updated SMTP to use Send-MailMessage, added DAG health check.\nV1.03, 04\/08\/2013 - Minor bug fixes\nV1.04, 19\/08\/2013 - Added Exchange 2013 compatibility, added option to output a log file, converted many\n                    sections of code to use pre-defined strings, fixed -AlertsOnly parameter, improved summary \n                    sections of report to be more readable and include DAG summary\nV1.05, 23\/08\/2013 - Added workaround for Test-ServiceHealth error for Exchange 2013 CAS-only servers\nV1.06, 28\/10\/2013 - Added workaround for Test-Mailflow error for Exchange 2013 Mailbox servers.\n                  - Added workaround for Exchange 2013 mail test.\n\t\t\t\t  - Added localization strings for service health check errors for non-English systems.\n\t\t\t\t  - Fixed an uptime calculation bug for some regional settings.\n\t\t\t\t  - Excluded recovery databases from active database calculation.\n\t\t\t\t  - Fixed bug where high transport queues would not count as an alert.\n                  - Fixed error thrown when Site attribute can't be found for Exchange 2003 servers.\n                  - Fixed bug causing Exchange 2003 servers to be added to the report twice.\nV1.07, 24\/11\/2013 - Fixed bug where disabled content indexes were counted as failed.\nV1.08, 29\/06\/2014 - Fixed bug with DAG reporting in mixed Exchange 2010\/2013 orgs.\nV1.09, 06\/07\/2014 - Fixed bug with DAG member replication health reporting for mixed Exchange 2010\/2013 orgs.\nV1.10, 19\/08\/2014 - Fixed bug with E14 replication health not testing correct server.\nV1.11, 11\/02\/2015 - Added queue length to Transport queue result in report.\nV1.12, 05\/03\/2015 - Fixed bug with color-coding in report for Transport Queue length.\nV1.13, 07\/03\/2015 - Fixed bug with incorrect function name used sometimes when trying to call Write-LogFile\nV1.14, 21\/05\/2015 - Fixed bug with color-coding in report for Transport Queue length on CAS-only Exchange 2013 servers.\nV1.15, 18\/11\/2015 - Fixed bug with Exchange 2016 version detection.\nV1.16, 13\/04\/2017 - Fixed bugs with recovery DB detection, invalid variables, shadow redundancy queues, and lagged copy detection.\nV1.17, 17\/05\/2017 - Fixed bug with auto-suspended content index detection\n#>\n\n#requires -version 2\n\n[CmdletBinding()]\nparam (\n        [Parameter( Mandatory=$false)]\n        [string]$Server,\n\n        [Parameter( Mandatory=$false)]\n        [string]$ServerList,    \n        \n        [Parameter( Mandatory=$false)]\n        [string]$ReportFile=\"exchangeserverhealth.html\",\n\n        [Parameter( Mandatory=$false)]\n        [switch]$ReportMode,\n        \n        [Parameter( Mandatory=$false)]\n        [switch]$SendEmail,\n\n        [Parameter( Mandatory=$false)]\n        [switch]$AlertsOnly,    \n        \n        [Parameter( Mandatory=$false)]\n        [switch]$Log\n    )\n\n\n#...................................\n# Variables\n#...................................\n\n$now = Get-Date                                             #Used for timestamps\n$date = $now.ToShortDateString()                            #Short date format for email message subject\n[array]$exchangeservers = @()                               #Array for the Exchange server or servers to check\n[int]$transportqueuehigh = 100                              #Change this to set transport queue high threshold. Must be higher than warning threshold.\n[int]$transportqueuewarn = 80                               #Change this to set transport queue warning threshold. Must be lower than high threshold.\n$mapitimeout = 10                                           #Timeout for each MAPI connectivity test, in seconds\n$pass = \"Green\"\n$warn = \"Yellow\"\n$fail = \"Red\"\n$ip = $null\n[array]$serversummary = @()                                 #Summary of issues found during server health checks\n[array]$dagsummary = @()                                    #Summary of issues found during DAG health checks\n[array]$report = @()\n[bool]$alerts = $false\n[array]$dags = @()                                          #Array for DAG health check\n[array]$dagdatabases = @()                                  #Array for DAG databases\n[int]$replqueuewarning = 8                                  #Threshold to consider a replication queue unhealthy\n$dagreportbody = $null\n\n$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path\n\n#...................................\n# Modify these Variables (optional)\n#...................................\n\n$reportemailsubject = \"Exchange Server Health Report\"\n$ignorelistfile = \"$myDir\\ignorelist.txt\"\n$logfile = \"$myDir\\exchangeserverhealth.log\"\n\n#...................................\n# Modify these Email Settings\n#...................................\n\n$smtpsettings = @{\n    To =  \"alan.lambert@trialcard.com\"\n    From = \"alan.lambert@trialcard.com\"\n    Subject = \"$reportemailsubject - $now\"\n    SmtpServer = \"relay.trialcard.com\"\n    }\n\n\n#...................................\n# Modify these language \n# localization strings.\n#...................................\n\n# The server roles must match the role names you see when you run Test-ServiceHealth.\n$casrole = \"Client Access Server Role\"\n$htrole = \"Hub Transport Server Role\"\n$mbrole = \"Mailbox Server Role\"\n$umrole = \"Unified Messaging Server Role\"\n\n# This should match the word for \"Success\", or the result of a successful Test-MAPIConnectivity test\n$success = \"Success\"\n\n#...................................\n# Logfile Strings\n#...................................\n\n$logstring0 = \"=====================================\"\n$logstring1 = \" Exchange Server Health Check\"\n\n#...................................\n# Initialization Strings\n#...................................\n\n$initstring0 = \"Initializing...\"\n$initstring1 = \"Loading the Exchange Server PowerShell snapin\"\n$initstring2 = \"The Exchange Server PowerShell snapin did not load.\"\n$initstring3 = \"Setting scope to entire forest\"\n\n#...................................\n# Error\/Warning Strings\n#...................................\n\n$string0 = \"Server is not an Exchange server. \"\n$string1 = \"Server is not reachable. \"\n$string3 = \"------ Checking\"\n$string4 = \"Could not test service health. \"\n$string5 = \"required services not running. \"\n$string6 = \"Could not check queue. \"\n$string7 = \"Public Folder database not mounted. \"\n$string8 = \"Skipping Edge Transport server. \"\n$string9 = \"Mailbox databases not mounted. \"\n$string10 = \"MAPI tests failed. \"\n$string11 = \"Mail flow test failed. \"\n$string12 = \"No Exchange Server 2003 checks performed. \"\n$string13 = \"Server not found in DNS. \"\n$string14 = \"Sending email. \"\n$string15 = \"Done.\"\n$string16 = \"------ Finishing\"\n$string17 = \"Unable to retrieve uptime. \"\n$string18 = \"Ping failed. \"\n$string19 = \"No alerts found, and AlertsOnly switch was used. No email sent. \"\n$string20 = \"You have specified a single server to check\"\n$string21 = \"Couldn't find the server $server. Script will terminate.\"\n$string22 = \"The file $ignorelistfile could not be found. No servers, DAGs or databases will be ignored.\"\n$string23 = \"You have specified a filename containing a list of servers to check\"\n$string24 = \"The file $serverlist could not be found. Script will terminate.\"\n$string25 = \"Retrieving server list\"\n$string26 = \"Removing servers in ignorelist from server list\"\n$string27 = \"Beginning the server health checks\"\n$string28 = \"Servers, DAGs and databases to ignore:\"\n$string29 = \"Servers to check:\"\n$string30 = \"Checking DNS\"\n$string31 = \"DNS check passed\"\n$string32 = \"Checking ping\"\n$string33 = \"Ping test passed\"\n$string34 = \"Checking uptime\"\n$string35 = \"Checking service health\"\n$string36 = \"Checking Hub Transport Server\"\n$string37 = \"Checking Mailbox Server\"\n$string38 = \"Ignore list contains no server names.\"\n$string39 = \"Checking public folder database\"\n$string40 = \"Public folder database status is\"\n$string41 = \"Checking mailbox databases\"\n$string42 = \"Mailbox database status is\"\n$string43 = \"Offline databases: \"\n$string44 = \"Checking MAPI connectivity\"\n$string45 = \"MAPI connectivity status is\"\n$string46 = \"MAPI failed to: \"\n$string47 = \"Checking mail flow\"\n$string48 = \"Mail flow status is\"\n$string49 = \"No active DBs\"\n$string50 = \"Finished checking server\"\n$string51 = \"Skipped\"\n$string52 = \"Using alternative test for Exchange 2013 CAS-only server\"\n$string60 = \"Beginning the DAG health checks\"\n$string61 = \"Could not determine server with active database copy\"\n$string62 = \"mounted on server that is activation preference\"\n$string63 = \"unhealthy database copy count is\"\n$string64 = \"healthy copy\/replay queue count is\"\n$string65 = \"(of\"\n$string66 = \")\"\n$string67 = \"unhealthy content index count is\"\n$string68 = \"DAGs to check:\"\n$string69 = \"DAG databases to check\"\n\n\n\n#...................................\n# Functions\n#...................................\n\n#This function is used to generate HTML for the DAG member health report\nFunction New-DAGMemberHTMLTableCell()\n{\n    param( $lineitem )\n    \n    $htmltablecell = $null\n\n    switch ($($line.\"$lineitem\"))\n    {\n        $null { $htmltablecell = \"&lt;td>n\/a&lt;\/td>\" }\n        \"Passed\" { $htmltablecell = \"&lt;td class=\"\"pass\"\">$($line.\"$lineitem\")&lt;\/td>\" }\n        default { $htmltablecell = \"&lt;td class=\"\"warn\"\">$($line.\"$lineitem\")&lt;\/td>\" }\n    }\n    \n    return $htmltablecell\n}\n\n#This function is used to generate HTML for the server health report\nFunction New-ServerHealthHTMLTableCell()\n{\n    param( $lineitem )\n    \n    $htmltablecell = $null\n    \n    switch ($($reportline.\"$lineitem\"))\n    {\n        $success {$htmltablecell = \"&lt;td class=\"\"pass\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Success\" {$htmltablecell = \"&lt;td class=\"\"pass\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Pass\" {$htmltablecell = \"&lt;td class=\"\"pass\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Warn\" {$htmltablecell = \"&lt;td class=\"\"warn\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Access Denied\" {$htmltablecell = \"&lt;td class=\"\"warn\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Fail\" {$htmltablecell = \"&lt;td class=\"\"fail\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Could not test service health. \" {$htmltablecell = \"&lt;td class=\"\"warn\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        \"Unknown\" {$htmltablecell = \"&lt;td class=\"\"warn\"\">$($reportline.\"$lineitem\")&lt;\/td>\"}\n        default {$htmltablecell = \"&lt;td>$($reportline.\"$lineitem\")&lt;\/td>\"}\n    }\n    \n    return $htmltablecell\n}\n\n#This function is used to write the log file if -Log is used\nFunction Write-Logfile()\n{\n    param( $logentry )\n    $timestamp = Get-Date -DisplayHint Time\n    \"$timestamp $logentry\" | Out-File $logfile -Append\n}\n\n#This function is used to test service health for Exchange 2013 CAS-only servers\nFunction Test-E15CASServiceHealth()\n{\n    param ( $e15cas )\n    \n    $e15casservicehealth = $null\n    $servicesrunning = @()\n    $servicesnotrunning = @()\n    $casservices = @(\n                    \"IISAdmin\",\n                    \"W3Svc\",\n                    \"WinRM\",\n                    \"MSExchangeADTopology\",\n                    \"MSExchangeDiagnostics\",\n                    \"MSExchangeFrontEndTransport\",\n                    #\"MSExchangeHM\",\n                    \"MSExchangeIMAP4\",\n                    \"MSExchangePOP3\",\n                    \"MSExchangeServiceHost\",\n                    \"MSExchangeUMCR\"\n                    )\n        \n    try {\n        $servicestates = @(Get-WmiObject -ComputerName $e15cas -Class Win32_Service -ErrorAction STOP | Where-Object {$casservices -icontains $_.Name} | Select-Object name,state,startmode)\n    }\n    catch\n    {\n        if ($Log) {Write-LogFile $_.Exception.Message}\n        Write-Warning $_.Exception.Message\n        $e15casservicehealth = \"Fail\"\n    }    \n    \n    if (!($e15casservicehealth))\n    {\n        $servicesrunning = @($servicestates | Where-Object {$_.StartMode -eq \"Auto\" -and $_.State -eq \"Running\"})\n        $servicesnotrunning = @($servicestates | Where-Object {$_.Startmode -eq \"Auto\" -and $_.State -ne \"Running\"})\n        if ($($servicesnotrunning.Count) -gt 0)\n        {\n            Write-Verbose \"Service health check failed\"\n            Write-Verbose \"Services not running:\"\n            foreach ($service in $servicesnotrunning)\n            {\n                Write-Verbose \"- $($service.Name)\"    \n            }\n            $e15casservicehealth = \"Fail\"    \n        }\n        else\n        {\n            Write-Verbose \"Service health check passed\"\n            $e15casservicehealth = \"Pass\"\n        }\n    }\n    return $e15casservicehealth\n}\n\n#This function is used to test mail flow for Exchange 2013 Mailbox servers\nFunction Test-E15MailFlow()\n{\n    param ( $e15mailboxserver )\n\n    $e15mailflowresult = $null\n    \n    Write-Verbose \"Creating PSSession for $e15mailboxserver\"\n    $url = (Get-PowerShellVirtualDirectory -Server $e15mailboxserver -AdPropertiesOnly | Where-Object {$_.Name -eq \"Powershell (Default Web Site)\"}).InternalURL.AbsoluteUri\n    if ($url -eq $null)\n    {\n        $url = \"http:\/\/$e15mailboxserver\/powershell\"\n    }\n\n    try\n    {\n        $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP\n    }\n    catch\n    {\n        Write-Verbose \"Something went wrong\"\n        if ($Log) {Write-LogFile $_.Exception.Message}\n        Write-Warning $_.Exception.Message\n        $e15mailflowresult = \"Fail\"\n    }\n\n    try\n    {\n        Write-Verbose \"Running mail flow test on $e15mailboxserver\"\n        $result = Invoke-Command -Session $session {Test-Mailflow} -ErrorAction STOP\n        $e15mailflowresult = $result.TestMailflowResult\n    }\n    catch\n    {\n        Write-Verbose \"An error occurred\"\n        if ($Log) {Write-LogFile $_.Exception.Message}\n        Write-Warning $_.Exception.Message\n        $e15mailflowresult = \"Fail\"\n    }\n\n    Write-Verbose \"Mail flow test: $e15mailflowresult\"\n    Write-Verbose \"Removing PSSession\"\n    Remove-PSSession $session.Id\n\n    return $e15mailflowresult\n}\n\n#This function is used to test replication health for Exchange 2010 DAG members in mixed 2010\/2013 organizations\nFunction Test-E14ReplicationHealth()\n{\n    param ( $e14mailboxserver )\n\n    $e14replicationhealth = $null\n    \n    #Find an E14 CAS in the same site\n    $ADSite = (Get-ExchangeServer $e14mailboxserver).Site\n    $e14cas = (Get-ExchangeServer | Where-Object {$_.IsClientAccessServer -and $_.AdminDisplayVersion -match \"Version 14\" -and $_.Site -eq $ADSite} | Select-Object -first 1).FQDN\n\n    Write-Verbose \"Creating PSSession for $e14cas\"\n    $url = (Get-PowerShellVirtualDirectory -Server $e14cas -AdPropertiesOnly | Where-Object {$_.Name -eq \"Powershell (Default Web Site)\"}).InternalURL.AbsoluteUri\n    if ($url -eq $null)\n    {\n        $url = \"http:\/\/$e14cas\/powershell\"\n    }\n\n    Write-Verbose \"Using URL $url\"\n\n    try\n    {\n        $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP\n    }\n    catch\n    {\n        Write-Verbose \"Something went wrong\"\n        if ($Log) {Write-LogFile $_.Exception.Message}\n        Write-Warning $_.Exception.Message\n        #$e14replicationhealth = \"Fail\"\n    }\n\n    try\n    {\n        Write-Verbose \"Running replication health test on $e14mailboxserver\"\n        #$e14replicationhealth = Invoke-Command -Session $session {Test-ReplicationHealth} -ErrorAction STOP\n        $e14replicationhealth = Invoke-Command -Session $session -Args $e14mailboxserver.Name {Test-ReplicationHealth $args[0]} -ErrorAction STOP\n    }\n    catch\n    {\n        Write-Verbose \"An error occurred\"\n        if ($Log) {Write-LogFile $_.Exception.Message}\n        Write-Warning $_.Exception.Message\n        #$e14replicationhealth = \"Fail\"\n    }\n\n    #Write-Verbose \"Replication health test: $e14replicationhealth\"\n    Write-Verbose \"Removing PSSession\"\n    Remove-PSSession $session.Id\n\n    return $e14replicationhealth\n}\n\n\n#...................................\n# Initialize\n#...................................\n\n#Log file is overwritten each time the script is run to avoid\n#very large log files from growing over time\nif ($Log) {\n    $timestamp = Get-Date -DisplayHint Time\n    \"$timestamp $logstring0\" | Out-File $logfile\n    Write-Logfile $logstring1\n    Write-Logfile \"  $now\"\n    Write-Logfile $logstring0\n}\n\nWrite-Host $initstring0\nif ($Log) {Write-Logfile $initstring0}\n\n#Add Exchange 2010 snapin if not already loaded in the PowerShell session\nif (!(Get-PSSnapin | Where-Object {$_.Name -eq \"Microsoft.Exchange.Management.PowerShell.E2010\"}))\n{\n    Write-Verbose $initstring1\n    if ($Log) {Write-Logfile $initstring1}\n    try\n    {\n        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP\n    }\n    catch\n    {\n        #Snapin was not loaded\n        Write-Verbose $initstring2\n        if ($Log) {Write-Logfile $initstring2}\n        Write-Warning $_.Exception.Message\n        EXIT\n    }\n    . $env:ExchangeInstallPath\\bin\\RemoteExchange.ps1\n    Connect-ExchangeServer -auto -AllowClobber\n}\n\n\n#Set scope to include entire forest\nWrite-Verbose $initstring3\nif ($Log) {Write-Logfile $initstring3}\nif (!(Get-ADServerSettings).ViewEntireForest)\n{\n    Set-ADServerSettings -ViewEntireForest $true -WarningAction SilentlyContinue\n}\n\n\n#...................................\n# Script\n#...................................\n\n#Check if a single server was specified\nif ($server)\n{\n    #Run for single specified server\n    [bool]$NoDAG = $true\n    Write-Verbose $string20\n    if ($Log) {Write-Logfile $string20}\n    try\n    {\n        $exchangeservers = Get-ExchangeServer $server -ErrorAction STOP\n    }\n    catch\n    {\n        #Exit because single server name was specified and couldn't be found in the organization\n        Write-Verbose $string21\n        if ($Log) {Write-Logfile $string21}\n        Write-Error $_.Exception.Message\n        EXIT\n    }\n}\nelseif ($serverlist)\n{\n    #Run for a list of servers in a text file\n    [bool]$NoDAG = $true\n    Write-Verbose $string23\n    if ($Log) {Write-Logfile $string23}\n    try\n    {\n        $tmpservers = @(Get-Content $serverlist -ErrorAction STOP)\n        $exchangeservers = @($tmpservers | Get-ExchangeServer)\n    }\n    catch\n    {\n        #Exit because file could not be found\n        Write-Verbose $string24\n        if ($Log) {Write-Logfile $string24}\n        Write-Error $_.Exception.Message\n        EXIT\n    }\n}\nelse\n{\n    #This is the list of servers, DAGs, and databases to never alert for\n    try\n    {\n        $ignorelist = @(Get-Content $ignorelistfile -ErrorAction STOP)\n        if ($Log) {Write-Logfile $string28}\n        if ($Log) {\n            if ($($ignorelist.count) -gt 0)\n            {\n                foreach ($line in $ignorelist)\n                {\n                    Write-Logfile \"- $line\"\n                }\n            }\n            else\n            {\n                Write-Logfile $string38\n            }\n        }\n    }\n    catch\n    {\n        Write-Warning $string22\n        if ($Log) {Write-Logfile $string22}\n    }\n    \n    #Get all servers\n    Write-Verbose $string25\n    if ($Log) {Write-Logfile $string25}\n    $GetExchangeServerResults = @(Get-ExchangeServer | Sort-Object site,name)\n    \n    #Remove the servers that are ignored from the list of servers to check\n    Write-Verbose $string26\n    if ($Log) {Write-Logfile $string26}\n    foreach ($tmpserver in $GetExchangeServerResults)\n    {\n        if (!($ignorelist -icontains $tmpserver.name))\n        {\n            $exchangeservers = $exchangeservers += $tmpserver.identity\n        }\n    }\n\n    if ($Log) {Write-Logfile $string29}\n    if ($Log) {\n        foreach ($server in $exchangeservers)\n        {\n            Write-Logfile \"- $server\"\n        }\n    }\n}\n\n### Check if any Exchange 2013 servers exist\nif ($GetExchangeServerResults | Where-Object {$_.AdminDisplayVersion -like \"Version 15.*\"})\n{\n    [bool]$HasE15 = $true\n}\n\n### Begin the Exchange Server health checks\nWrite-Verbose $string27\nif ($Log) {Write-Logfile $string27}\nforeach ($server in $exchangeservers)\n{\n    Write-Host -ForegroundColor White \"$string3 $server\"\n    if ($Log) {Write-Logfile \"$string3 $server\"}\n    \n    #Find out some details about the server\n    try\n    {\n        $serverinfo = Get-ExchangeServer $server -ErrorAction Stop\n    }\n    catch\n    {\n        Write-Warning $_.Exception.Message\n        if ($Log) {Write-Logfile $_.Exception.Message}\n        $serverinfo = $null\n    }\n\n    if ($serverinfo -eq $null )\n    {\n        #Server is not an Exchange server\n        Write-Host -ForegroundColor $warn $string0\n        if ($Log) {Write-Logfile $string0}\n    }\n    elseif ( $serverinfo.IsEdgeServer )\n    {\n        Write-Host -ForegroundColor White $string8\n        if ($Log) {Write-Logfile $string8}\n    }\n    else\n    {\n        #Server is an Exchange server, continue the health check\n\n        #Custom object properties\n        $serverObj = New-Object PSObject\n        $serverObj | Add-Member NoteProperty -Name \"Server\" -Value $server\n        \n        #Skip Site attribute for Exchange 2003 servers\n        if ($serverinfo.AdminDisplayVersion -like \"Version 6.*\")\n        {\n            $serverObj | Add-Member NoteProperty -Name \"Site\" -Value \"n\/a\"\n        }\n        else\n        {\n            $site = ($serverinfo.site.ToString()).Split(\"\/\")\n            $serverObj | Add-Member NoteProperty -Name \"Site\" -Value $site[-1]\n        }\n        \n        #Null and n\/a the rest, will be populated as script progresses\n        $serverObj | Add-Member NoteProperty -Name \"DNS\" -Value $null\n        $serverObj | Add-Member NoteProperty -Name \"Ping\" -Value $null\n        $serverObj | Add-Member NoteProperty -Name \"Uptime (hrs)\" -Value $null\n        $serverObj | Add-Member NoteProperty -Name \"Version\" -Value $null\n        $serverObj | Add-Member NoteProperty -Name \"Roles\" -Value $null\n        $serverObj | Add-Member NoteProperty -Name \"Client Access Server Role Services\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Hub Transport Server Role Services\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Mailbox Server Role Services\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Unified Messaging Server Role Services\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Transport Queue\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Queue Length\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"PF DBs Mounted\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"MB DBs Mounted\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value \"n\/a\"\n        $serverObj | Add-Member NoteProperty -Name \"MAPI Test\" -Value \"n\/a\"\n\n        #Check server name resolves in DNS\n        if ($Log) {Write-Logfile $string30}\n        Write-Host \"DNS Check: \" -NoNewline;\n        try \n        {\n            $ip = @([System.Net.Dns]::GetHostByName($server).AddressList | Select-Object IPAddressToString -ExpandProperty IPAddressToString)\n        }\n        catch\n        {\n            Write-Host -ForegroundColor $warn $_.Exception.Message\n            if ($Log) {Write-Logfile $_.Exception.Message}\n            $ip = $null\n        }\n\n        if ( $ip -ne $null )\n        {\n            Write-Host -ForegroundColor $pass \"Pass\"\n            if ($Log) {Write-Logfile $string31}\n            $serverObj | Add-Member NoteProperty -Name \"DNS\" -Value \"Pass\" -Force\n\n            #Is server online\n            if ($Log) {Write-Logfile $string32}\n            Write-Host \"Ping Check: \" -NoNewline; \n            \n            $ping = $null\n            try\n            {\n                $ping = Test-Connection $server -Quiet -ErrorAction Stop\n            }\n            catch\n            {\n                Write-Host -ForegroundColor $warn $_.Exception.Message\n                if ($Log) {Write-Logfile $_.Exception.Message}\n            }\n\n            switch ($ping)\n            {\n                $true {\n                    Write-Host -ForegroundColor $pass \"Pass\"\n                    $serverObj | Add-Member NoteProperty -Name \"Ping\" -Value \"Pass\" -Force\n                    if ($Log) {Write-Logfile $string33}\n                    }\n                default {\n                    Write-Host -ForegroundColor $fail \"Fail\"\n                    $serverObj | Add-Member NoteProperty -Name \"Ping\" -Value \"Fail\" -Force\n                    $serversummary += \"$server - $string18\"\n                    if ($Log) {Write-Logfile $string18}\n                    }\n            }\n            \n            #Uptime check, even if ping fails\n            if ($Log) {Write-Logfile $string34}\n            [int]$uptime = $null\n            #$laststart = $null\n            $OS = $null\n        \n            try \n            {\n                #$laststart = [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem -computername $server -ErrorAction Stop).LastBootUpTime)\n                $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server -ErrorAction STOP\n            }\n            catch\n            {\n                Write-Host -ForegroundColor $warn $_.Exception.Message\n                if ($Log) {Write-Logfile $_.Exception.Message}\n            }\n            \n            Write-Host \"Uptime (hrs): \" -NoNewline\n\n            if ($OS -eq $null)\n            {\n                [string]$uptime = $string17\n                if ($Log) {Write-Logfile $string17}\n                switch ($ping)\n                {\n                    $true {    $serversummary += \"$server - $string17\" }\n                    default { $serversummary += \"$server - $string17\" }\n                }\n            }\n            else\n            {\n                $timespan = $OS.ConvertToDateTime($OS.LocalDateTime) \u2013 $OS.ConvertToDateTime($OS.LastBootUpTime)\n                [int]$uptime = \"{0:00}\" -f $timespan.TotalHours\n                Switch ($uptime -gt 23) {\n                    $true { Write-Host -ForegroundColor $pass $uptime }\n                    $false { Write-Host -ForegroundColor $warn $uptime; $serversummary += \"$server - Uptime is less than 24 hours\" }\n                    default { Write-Host -ForegroundColor $warn $uptime; $serversummary += \"$server - Uptime is less than 24 hours\" }\n                }\n            }\n\n            if ($Log) {Write-Logfile \"Uptime is $uptime hours\"}\n\n            $serverObj | Add-Member NoteProperty -Name \"Uptime (hrs)\" -Value $uptime -Force    \n            \n            if ($ping -or ($uptime -ne $string17))\n            {\n                #Determine the friendly version number\n                $ExVer = $serverinfo.AdminDisplayVersion\n                Write-Host \"Server version: \" -NoNewline;\n                \n                if ($ExVer -like \"Version 6.*\")\n                {\n                    $version = \"Exchange 2003\"\n                }\n                \n                if ($ExVer -like \"Version 8.*\")\n                {\n                    $version = \"Exchange 2007\"\n                }\n                \n                if ($ExVer -like \"Version 14.*\")\n                {\n                    $version = \"Exchange 2010\"\n                }\n                \n                if ($ExVer -like \"Version 15.0*\")\n                {\n                    $version = \"Exchange 2013\"\n                }\n\n                if ($ExVer -like \"Version 15.1*\")\n                {\n                    $version = \"Exchange 2016\"\n                }\n                \n                Write-Host $version\n                if ($Log) {Write-Logfile \"Server is running $version\"}\n                $serverObj | Add-Member NoteProperty -Name \"Version\" -Value $version -Force\n            \n                if ($version -eq \"Exchange 2003\")\n                {\n                    Write-Host $string12\n                    if ($Log) {Write-Logfile $string12}\n                }\n\n                #START - Exchange 2013\/2010\/2007 Health Checks\n                if ($version -ne \"Exchange 2003\")\n                {\n                    Write-Host \"Roles:\" $serverinfo.ServerRole\n                    if ($Log) {Write-Logfile \"Server roles: $($serverinfo.ServerRole)\"}\n                    $serverObj | Add-Member NoteProperty -Name \"Roles\" -Value $serverinfo.ServerRole -Force\n                    \n                    $IsEdge = $serverinfo.IsEdgeServer        \n                    $IsHub = $serverinfo.IsHubTransportServer\n                    $IsCAS = $serverinfo.IsClientAccessServer\n                    $IsMB = $serverinfo.IsMailboxServer\n\n                    #START - General Server Health Check\n                    #Skipping Edge Transports for the general health check, as firewalls usually get\n                    #in the way. If you want to include them, remove this If.\n                    if ($IsEdge -ne $true)\n                    {\n                        #Service health is an array due to how multi-role servers return Test-ServiceHealth status\n                        if ($Log) {Write-Logfile $string35}\n                        $servicehealth = @()\n                        $e15casservicehealth = @()\n                        try {\n                            $servicehealth = @(Test-ServiceHealth $server -ErrorAction Stop)\n                        }\n                        catch {\n                            #Workaround for Test-ServiceHealth problem with CAS-only Exchange 2013 servers\n                            #More info: http:\/\/exchangeserverpro.com\/exchange-2013-test-servicehealth-error\/\n                            if ($_.Exception.Message -like \"*There are no Microsoft Exchange 2007 server roles installed*\")\n                            {\n                                if ($Log) {Write-Logfile $string52}\n                                $e15casservicehealth = Test-E15CASServiceHealth($server)\n                            }\n                            else\n                            {\n                                $serversummary += \"$server - $string4\"\n                                Write-Host -ForegroundColor $warn $string4 \":\" $_.Exception\n                                if ($Log) {Write-Logfile $_.Exception}\n                                $serverObj | Add-Member NoteProperty -Name \"Client Access Server Role Services\" -Value $string4 -Force\n                                $serverObj | Add-Member NoteProperty -Name \"Hub Transport Server Role Services\" -Value $string4 -Force\n                                $serverObj | Add-Member NoteProperty -Name \"Mailbox Server Role Services\" -Value $string4 -Force\n                                $serverObj | Add-Member NoteProperty -Name \"Unified Messaging Server Role Services\" -Value $string4 -Force\n                            }\n                        }\n                            \n                        if ($servicehealth)\n                        {\n                            foreach($s in $servicehealth)\n                            {\n                                $roleName = $s.Role\n                                Write-Host $roleName \"Services: \" -NoNewline;\n                                                            \n                                switch ($s.RequiredServicesRunning)\n                                {\n                                    $true {\n                                            $svchealth = \"Pass\"\n                                            Write-Host -ForegroundColor $pass \"Pass\"\n                                        }\n                                    $false {\n                                            $svchealth = \"Fail\"\n                                            Write-Host -ForegroundColor $fail \"Fail\"\n                                            $serversummary += \"$server - $rolename $string5\"\n                                        }\n                                    default {\n                                            $svchealth = \"Warn\"\n                                            Write-Host -ForegroundColor $warn \"Warning\"\n                                            $serversummary += \"$server - $rolename $string5\"\n                                        }\n                                }\n\n                                switch ($s.Role)\n                                {\n                                    $casrole { $serverinfoservices = \"Client Access Server Role Services\" }\n                                    $htrole { $serverinfoservices = \"Hub Transport Server Role Services\" }\n                                    $mbrole { $serverinfoservices = \"Mailbox Server Role Services\" }\n                                    $umrole { $serverinfoservices = \"Unified Messaging Server Role Services\" }\n                                }\n                                if ($Log) {Write-Logfile \"$serverinfoservices status is $svchealth\"}    \n                                $serverObj | Add-Member NoteProperty -Name $serverinfoservices -Value $svchealth -Force\n                            }\n                        }\n                        \n                        if ($e15casservicehealth)\n                        {\n                            $serverinfoservices = \"Client Access Server Role Services\"\n                            if ($Log) {Write-Logfile \"$serverinfoservices status is $e15casservicehealth\"}\n                            $serverObj | Add-Member NoteProperty -Name $serverinfoservices -Value $e15casservicehealth -Force\n                            Write-Host $serverinfoservices \": \" -NoNewline;\n                            switch ($e15casservicehealth)\n                            {\n                                \"Pass\" { Write-Host -ForegroundColor $pass \"Pass\" }\n                                \"Fail\" { Write-Host -ForegroundColor $fail \"Fail\" }\n                            }\n                        }\n                    }\n                    #END - General Server Health Check\n\n                    #START - Hub Transport Server Check\n                    if ($IsHub)\n                    {\n                        $q = $null\n                        if ($Log) {Write-Logfile $string36}\n                        Write-Host \"Total Queue: \" -NoNewline; \n                        try {\n                            $q = Get-Queue -server $server -ErrorAction Stop | Where-Object {$_.DeliveryType -ne \"ShadowRedundancy\"}\n                        }\n                        catch {\n                            $serversummary += \"$server - $string6\"\n                            Write-Host -ForegroundColor $warn $string6\n                            Write-Warning $_.Exception.Message\n                            if ($Log) {Write-Logfile $string6}\n                            if ($Log) {Write-Logfile $_.Exception.Message}\n                        }\n                        \n                        if ($q)\n                        {\n                            $qcount = $q | Measure-Object MessageCount -Sum\n                            [int]$qlength = $qcount.sum\n                            $serverObj | Add-Member NoteProperty -Name \"Queue Length\" -Value $qlength -Force\n                            if ($Log) {Write-Logfile \"Queue length is $qlength\"}\n                            if ($qlength -le $transportqueuewarn)\n                            {\n                                Write-Host -ForegroundColor $pass $qlength\n                                $serverObj | Add-Member NoteProperty -Name \"Transport Queue\" -Value \"Pass ($qlength)\" -Force\n                            }\n                            elseif ($qlength -gt $transportqueuewarn -and $qlength -lt $transportqueuehigh)\n                            {\n                                Write-Host -ForegroundColor $warn $qlength\n                                $serversummary += \"$server - Transport queue is above warning threshold\" \n                                $serverObj | Add-Member NoteProperty -Name \"Transport Queue\" -Value \"Warn ($qlength)\" -Force\n                            }\n                            else\n                            {\n                                Write-Host -ForegroundColor $fail $qlength\n                                $serversummary += \"$server - Transport queue is above high threshold\"\n                                $serverObj | Add-Member NoteProperty -Name \"Transport Queue\" -Value \"Fail ($qlength)\" -Force\n                            }\n                        }\n                        else\n                        {\n                            $serverObj | Add-Member NoteProperty -Name \"Transport Queue\" -Value \"Unknown\" -Force\n                        }\n                    }\n                    #END - Hub Transport Server Check\n\n                    #START - Mailbox Server Check\n                    if ($IsMB)\n                    {\n                        if ($Log) {Write-Logfile $string37}\n                        \n                        #Get the PF and MB databases\n                        [array]$pfdbs = @(Get-PublicFolderDatabase -server $server -status -WarningAction SilentlyContinue)\n                        [array]$mbdbs = @(Get-MailboxDatabase -server $server -status | Where-Object {$_.Recovery -ne $true})\n                        \n                        if ($version -ne \"Exchange 2007\")\n                        {\n                            [array]$activedbs = @(Get-MailboxDatabase -server $server -status | Where-Object {$_.Recovery -ne $true -and $_.MountedOnServer -eq ($serverinfo.fqdn)})\n                        }\n                        else\n                        {\n                            [array]$activedbs = $mbdbs\n                        }\n                        \n                        #START - Database Mount Check\n                        \n                        #Check public folder databases\n                        if ($pfdbs.count -gt 0)\n                        {\n                            if ($Log) {Write-Logfile $string39}\n                            Write-Host \"Public Folder databases mounted: \" -NoNewline;\n                            [string]$pfdbstatus = \"Pass\"\n                            [array]$alertdbs = @()\n                            foreach ($db in $pfdbs)\n                            {\n                                if (($db.mounted) -ne $true)\n                                {\n                                    $pfdbstatus = \"Fail\"\n                                    $alertdbs += $db.name\n                                }\n                            }\n\n                            $serverObj | Add-Member NoteProperty -Name \"PF DBs Mounted\" -Value $pfdbstatus -Force\n                            if ($Log) {Write-Logfile \"$string40 $pfdbstatus\"}\n                            \n                            if ($alertdbs.count -eq 0)\n                            {\n                                Write-Host -ForegroundColor $pass $pfdbstatus\n                            }\n                            else\n                            {\n                                Write-Host -ForegroundColor $fail $pfdbstatus\n                                $serversummary += \"$server - $string7\"\n                                Write-Host \"Offline databases:\"\n                                foreach ($al in $alertdbs)\n                                {\n                                    Write-Host -ForegroundColor $fail `t$al\n                                }\n                            }\n                        }\n                        \n                        #Check mailbox databases\n                        if ($mbdbs.count -gt 0)\n                        {\n                            if ($Log) {Write-Logfile $string41}\n                        \n                            [string]$mbdbstatus = \"Pass\"\n                            [array]$alertdbs = @()\n\n                            Write-Host \"Mailbox databases mounted: \" -NoNewline;\n                            foreach ($db in $mbdbs)\n                            {\n                                if (($db.mounted) -ne $true)\n                                {\n                                    $mbdbstatus = \"Fail\"\n                                    $alertdbs += $db.name\n                                }\n                            }\n\n                            $serverObj | Add-Member NoteProperty -Name \"MB DBs Mounted\" -Value $mbdbstatus -Force\n                            if ($Log) {Write-Logfile \"$string42 $mbdbstatus\"}\n                            \n                            if ($alertdbs.count -eq 0)\n                            {\n                                Write-Host -ForegroundColor $pass $mbdbstatus\n                            }\n                            else\n                            {\n                                $serversummary += \"$server - $string9\"\n                                Write-Host -ForegroundColor $fail $mbdbstatus\n                                Write-Host $string43\n                                if ($Log) {Write-Logfile $string43}\n                                foreach ($al in $alertdbs)\n                                {\n                                    Write-Host -ForegroundColor $fail `t$al\n                                    if ($Log) {Write-Logfile \"- $al\"}\n                                }\n                            }\n                        }\n                        \n                        #END - Database Mount Check\n                        \n                        #START - MAPI Connectivity Test\n                        if ($activedbs.count -gt 0 -or $pfdbs.count -gt 0 -or $version -eq \"Exchange 2007\")\n                        {\n                            [string]$mapiresult = \"Unknown\"\n                            [array]$alertdbs = @()\n                            if ($Log) {Write-Logfile $string44}\n                            Write-Host \"MAPI connectivity: \" -NoNewline;\n                            foreach ($db in $mbdbs)\n                            {\n                                $mapistatus = Test-MapiConnectivity -Database $db.Identity -PerConnectionTimeout $mapitimeout\n                                if ($mapistatus.Result.Value -eq $null)\n                                {\n                                    $mapiresult = $mapistatus.Result\n                                }\n                                else\n                                {\n                                    $mapiresult = $mapistatus.Result.Value\n                                }\n                                if (($mapiresult) -ne \"Success\")\n                                {\n                                    $mapistatus = \"Fail\"\n                                    $alertdbs += $db.name\n                                }\n                            }\n\n                            $serverObj | Add-Member NoteProperty -Name \"MAPI Test\" -Value  $mapiresult -Force\n                            if ($Log) {Write-Logfile \"$string45  $mapiresult\"}\n                            \n                            if ($alertdbs.count -eq 0)\n                            {\n                                Write-Host -ForegroundColor $pass  $mapiresult\n                            }\n                            else\n                            {\n                                $serversummary += \"$server - $string10\"\n                                Write-Host -ForegroundColor $fail  $mapiresult\n                                Write-Host $string46\n                                if ($Log) {Write-Logfile $string46}\n                                foreach ($al in $alertdbs)\n                                {\n                                    Write-Host -ForegroundColor $fail `t$al\n                                    if ($Log) {Write-Logfile \"- $al\"}\n                                }\n                            }\n                        }\n                        #END - MAPI Connectivity Test\n                        \n                        #START - Mail Flow Test\n                        if ($version -eq \"Exchange 2007\" -and $mbdbs.count -gt 0 -and $HasE15)\n                        {\n                            #Skip Exchange 2007 mail flow tests when run from Exchange 2013\n                            if ($Log) {Write-Logfile $string47}\n                            Write-Host \"Mail flow test: Skipped\"\n                            $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value $string51 -Force\n                            if ($Log) {Write-Logfile $string51}\n                        }\n                        elseif ($activedbs.count -gt 0 -and $HasE15)\n                        {\n                            if ($Log) {Write-Logfile $string47}\n                            Write-Host \"Mail flow test: \" -NoNewline;\n                            $e15mailflowresult = Test-E15MailFlow($Server)\n                            $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value $e15mailflowresult -Force\n                            if ($Log) {Write-Logfile \"$string48 $e15mailflowresult\"}\n                            \n                            if ($e15mailflowresult -eq $success)\n                            {\n                                Write-Host -ForegroundColor $pass $e15mailflowresult\n                                $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value \"Pass\" -Force\n                            }\n                            else\n                            {\n                                $serversummary += \"$server - $string11\"\n                                Write-Host -ForegroundColor $fail $e15mailflowresult\n                                $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value \"Fail\" -Force\n                            }\n                        }\n                        elseif ($activedbs.count -gt 0 -or ($version -eq \"Exchange 2007\" -and $mbdbs.count -gt 0))\n                        {\n                            $flow = $null\n                            $testmailflowresult = $null\n                            \n                            if ($Log) {Write-Logfile $string47}\n                            Write-Host \"Mail flow test: \" -NoNewline;\n                            try\n                            {\n                                $flow = Test-Mailflow $server -ErrorAction Stop\n                            }\n                            catch\n                            {\n                                $testmailflowresult = $_.Exception.Message\n                                if ($Log) {Write-Logfile $_.Exception.Message}\n                            }\n                            \n                            if ($flow)\n                            {\n                                $testmailflowresult = $flow.testmailflowresult\n                                if ($Log) {Write-Logfile \"$string48 $testmailflowresult\"}\n                            }\n\n                            if ($testmailflowresult -eq \"Success\" -or $testmailflowresult -eq $success)\n                            {\n                                Write-Host -ForegroundColor $pass $testmailflowresult\n                                $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value \"Pass\" -Force\n                            }\n                            else\n                            {\n                                $serversummary += \"$server - $string11\"\n                                Write-Host -ForegroundColor $fail $testmailflowresult\n                                $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value \"Fail\" -Force\n                            }\n                        }\n                        else\n                        {\n                            Write-Host \"Mail flow test: No active mailbox databases\"\n                            $serverObj | Add-Member NoteProperty -Name \"Mail Flow Test\" -Value $string49 -Force\n                            if ($Log) {Write-Logfile $string49}\n                        }\n                        #END - Mail Flow Test\n                    }\n                    #END - Mailbox Server Check\n\n                }\n                #END - Exchange 2013\/2010\/2007 Health Checks\n                if ($Log) {Write-Logfile \"$string50 $server\"}\n                $report = $report + $serverObj\n            }\n            else\n            {\n                #Server is not reachable and uptime could not be retrieved\n                Write-Host -ForegroundColor $warn $string1\n                if ($Log) {Write-Logfile $string1}\n                $serversummary += \"$server - $string1\"\n                $serverObj | Add-Member NoteProperty -Name \"Ping\" -Value \"Fail\" -Force\n                if ($Log) {Write-Logfile \"$string50 $server\"}\n                $report = $report + $serverObj\n            }\n        }\n        else\n        {\n            Write-Host -ForegroundColor $Fail \"Fail\"\n            Write-Host -ForegroundColor $warn $string13\n            if ($Log) {Write-Logfile $string13}\n            $serversummary += \"$server - $string13\"\n            $serverObj | Add-Member NoteProperty -Name \"DNS\" -Value \"Fail\" -Force\n            if ($Log) {Write-Logfile \"$string50 $server\"}\n            $report = $report + $serverObj\n        }\n    }    \n}\n### End the Exchange Server health checks\n\n\n### Begin DAG Health Report\n\n#Check if -Server or -Serverlist parameter was used, and skip if it was\nif (!($NoDAG))\n{\n    if ($Log) {Write-Logfile $string60}\n    Write-Verbose \"Retrieving Database Availability Groups\"\n\n    #Get all DAGs\n    $tmpdags = @(Get-DatabaseAvailabilityGroup)\n    $tmpstring = \"$($tmpdags.count) DAGs found\"\n    Write-Verbose $tmpstring\n    if ($Log) {Write-Logfile $tmpstring}\n\n    #Remove DAGs in ignorelist\n    foreach ($tmpdag in $tmpdags)\n    {\n        if (!($ignorelist -icontains $tmpdag.name))\n        {\n            $dags += $tmpdag\n        }\n    }\n\n    $tmpstring = \"$($dags.count) DAGs will be checked\"\n    Write-Verbose $tmpstring\n    if ($Log) {Write-Logfile $tmpstring}\n\n    if ($Log) {Write-Logfile $string68}\n    if ($Log) {\n        foreach ($dag in $dags)\n        {\n            Write-Logfile \"- $dag\"\n        }\n    }\n}\n\nif ($($dags.count) -gt 0)\n{\n    foreach ($dag in $dags)\n    {\n        \n        #Strings for use in the HTML report\/email\n        $dagsummaryintro = \"&lt;p>Database Availability Group &lt;strong>$($dag.Name)&lt;\/strong> Health Summary:&lt;\/p>\"\n        $dagdetailintro = \"&lt;p>Database Availability Group &lt;strong>$($dag.Name)&lt;\/strong> Health Details:&lt;\/p>\"\n        $dagmemberintro = \"&lt;p>Database Availability Group &lt;strong>$($dag.Name)&lt;\/strong> Member Health:&lt;\/p>\"\n\n        $dagdbcopyReport = @()      #Database copy health report\n        $dagciReport = @()          #Content Index health report\n        $dagmemberReport = @()      #DAG member server health report\n        $dagdatabaseSummary = @()   #Database health summary report\n        $dagdatabases = @()         #Array of databases in the DAG\n        \n        $tmpstring = \"---- Processing DAG $($dag.Name)\"\n        Write-Verbose $tmpstring\n        if ($Log) {Write-Logfile $tmpstring}\n        \n        $dagmembers = @($dag | Select-Object -ExpandProperty Servers | Sort-Object Name)\n        $tmpstring = \"$($dagmembers.count) DAG members found\"\n        Write-Verbose $tmpstring\n        if ($Log) {Write-Logfile $tmpstring}\n        \n        #Get all databases in the DAG\n        if ($HasE15)\n        {\n            $tmpdatabases = @(Get-MailboxDatabase -Status -IncludePreExchange2013 | Where-Object {$_.Recovery -ne $true -and $_.MasterServerOrAvailabilityGroup -eq $dag.Name} | Sort-Object Name)\n        }\n        else\n        {\n            $tmpdatabases = @(Get-MailboxDatabase -Status | Where-Object {$_.Recovery -ne $true -and $_.MasterServerOrAvailabilityGroup -eq $dag.Name} | Sort-Object Name)\n        }\n\n        foreach ($tmpdatabase in $tmpdatabases)\n        {\n            if (!($ignorelist -icontains $tmpdatabase.name))\n            {\n                $dagdatabases += $tmpdatabase\n            }\n        }\n                \n        $tmpstring = \"$($dagdatabases.count) DAG databases will be checked\"\n        Write-Verbose $tmpstring\n        if ($Log) {Write-Logfile $tmpstring}\n\n        if ($Log) {Write-Logfile $string69}\n        if ($Log) {\n            foreach ($database in $dagdatabases)\n            {\n                Write-Logfile \"- $database\"\n            }\n        }\n        \n        foreach ($database in $dagdatabases)\n        {\n            $tmpstring = \"---- Processing database $database\"\n            Write-Verbose $tmpstring\n            if ($Log) {Write-Logfile $tmpstring}\n\n            $activationPref = $null\n            $totalcopies = $null\n            $healthycopies = $null\n            $unhealthycopies = $null\n            $healthyqueues  = $null\n            $unhealthyqueues = $null\n            $laggedqueues = $null\n            $healthyindexes = $null\n            $unhealthyindexes = $null\n\n            #Custom object for Database\n            $objectHash = @{\n                            \"Database\" = $database.Identity\n                            \"Mounted on\" = \"Unknown\"\n                            \"Preference\" = $null\n                            \"Total Copies\" = $null\n                            \"Healthy Copies\" = $null\n                            \"Unhealthy Copies\" = $null\n                            \"Healthy Queues\" = $null\n                            \"Unhealthy Queues\" = $null\n                            \"Lagged Queues\" = $null\n                            \"Healthy Indexes\" = $null\n                            \"Unhealthy Indexes\" = $null\n                            }\n            $databaseObj = New-Object PSObject -Property $objectHash\n\n            $dbcopystatus = @($database | Get-MailboxDatabaseCopyStatus)\n            $tmpstring = \"$database has $($dbcopystatus.Count) copies\"\n            Write-Verbose $tmpstring\n            if ($Log) {Write-Logfile $tmpstring}\n            \n            foreach ($dbcopy in $dbcopystatus)\n            {\n                #Custom object for DB copy\n                $objectHash = @{\n                                \"Database Copy\" = $dbcopy.Identity\n                                \"Database Name\" = $dbcopy.DatabaseName\n                                \"Mailbox Server\" = $null\n                                \"Activation Preference\" = $null\n                                \"Status\" = $null\n                                \"Copy Queue\" = $null\n                                \"Replay Queue\" = $null\n                                \"Replay Lagged\" = $null\n                                \"Truncation Lagged\" = $null\n                                \"Content Index\" = $null\n                                }\n                $dbcopyObj = New-Object PSObject -Property $objectHash\n                \n                $tmpstring = \"Database Copy: $($dbcopy.Identity)\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                \n                $mailboxserver = $dbcopy.MailboxServer\n                $tmpstring = \"Server: $mailboxserver\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n\n                $pref = ($database | Select-Object -ExpandProperty ActivationPreference | Where-Object {$_.Key -ieq $mailboxserver}).Value\n                $tmpstring = \"Activation Preference: $pref\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n\n                $copystatus = $dbcopy.Status\n                $tmpstring = \"Status: $copystatus\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                \n                [int]$copyqueuelength = $dbcopy.CopyQueueLength\n                $tmpstring = \"Copy Queue: $copyqueuelength\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                \n                [int]$replayqueuelength = $dbcopy.ReplayQueueLength\n                $tmpstring = \"Replay Queue: $replayqueuelength\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                \n                if ($($dbcopy.ContentIndexErrorMessage -match \"is disabled in Active Directory\"))\n                {\n                    $contentindexstate = \"Disabled\"\n                }\n                else\n                {\n                    $contentindexstate = $dbcopy.ContentIndexState\n                }\n                $tmpstring = \"Content Index: $contentindexstate\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}                \n\n                #Checking whether this is a replay lagged copy\n                $replaylagcopies = @($database | Select-Object -ExpandProperty ReplayLagTimes | Where-Object {$_.Value -gt 0})\n                if ($($replaylagcopies.count) -gt 0)\n                {\n                    [bool]$replaylag = $false\n                    foreach ($replaylagcopy in $replaylagcopies)\n                    {\n                        if ($replaylagcopy.Key -ieq $mailboxserver)\n                        {\n                            $tmpstring = \"$database is replay lagged on $mailboxserver\"\n                            Write-Verbose $tmpstring\n                            if ($Log) {Write-Logfile $tmpstring}\n                            [bool]$replaylag = $true\n                        }\n                    }\n                }\n                else\n                {\n                   [bool]$replaylag = $false\n                }\n                $tmpstring = \"Replay lag is $replaylag\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}                \n                        \n                #Checking for truncation lagged copies\n                $truncationlagcopies = @($database | Select-Object -ExpandProperty TruncationLagTimes | Where-Object {$_.Value -gt 0})\n                if ($($truncationlagcopies.count) -gt 0)\n                {\n                    [bool]$truncatelag = $false\n                    foreach ($truncationlagcopy in $truncationlagcopies)\n                    {\n                        if ($truncationlagcopy.Key -eq $mailboxserver)\n                        {\n                            $tmpstring = \"$database is truncate lagged on $mailboxserver\"\n                            Write-Verbose $tmpstring\n                            if ($Log) {Write-Logfile $tmpstring}                            \n                            [bool]$truncatelag = $true\n                        }\n                    }\n                }\n                else\n                {\n                   [bool]$truncatelag = $false\n                }\n                $tmpstring = \"Truncation lag is $truncatelag\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                \n                $dbcopyObj | Add-Member NoteProperty -Name \"Mailbox Server\" -Value $mailboxserver -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Activation Preference\" -Value $pref -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Status\" -Value $copystatus -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Copy Queue\" -Value $copyqueuelength -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Replay Queue\" -Value $replayqueuelength -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Replay Lagged\" -Value $replaylag -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Truncation Lagged\" -Value $truncatelag -Force\n                $dbcopyObj | Add-Member NoteProperty -Name \"Content Index\" -Value $contentindexstate -Force\n                \n                $dagdbcopyReport += $dbcopyObj\n            }\n        \n            $copies = @($dagdbcopyReport | Where-Object { ($_.\"Database Name\" -eq $database) })\n        \n            $mountedOn = ($copies | Where-Object { ($_.Status -eq \"Mounted\") }).\"Mailbox Server\"\n            if ($mountedOn)\n            {\n                $databaseObj | Add-Member NoteProperty -Name \"Mounted on\" -Value $mountedOn -Force\n            }\n        \n            $activationPref = ($copies | Where-Object { ($_.Status -eq \"Mounted\") }).\"Activation Preference\"\n            $databaseObj | Add-Member NoteProperty -Name \"Preference\" -Value $activationPref -Force\n\n            $totalcopies = $copies.count\n            $databaseObj | Add-Member NoteProperty -Name \"Total Copies\" -Value $totalcopies -Force\n        \n            $healthycopies = @($copies | Where-Object { (($_.Status -eq \"Mounted\") -or ($_.Status -eq \"Healthy\")) }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Healthy Copies\" -Value $healthycopies -Force\n            \n            $unhealthycopies = @($copies | Where-Object { (($_.Status -ne \"Mounted\") -and ($_.Status -ne \"Healthy\")) }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Unhealthy Copies\" -Value $unhealthycopies -Force\n\n            $healthyqueues  = @($copies | Where-Object { (($_.\"Copy Queue\" -lt $replqueuewarning) -and (($_.\"Replay Queue\" -lt $replqueuewarning)) -and ($_.\"Replay Lagged\" -eq $false)) }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Healthy Queues\" -Value $healthyqueues -Force\n\n            $unhealthyqueues = @($copies | Where-Object { (($_.\"Copy Queue\" -ge $replqueuewarning) -or (($_.\"Replay Queue\" -ge $replqueuewarning) -and ($_.\"Replay Lagged\" -eq $false))) }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Unhealthy Queues\" -Value $unhealthyqueues -Force\n\n            $laggedqueues = @($copies | Where-Object { ($_.\"Replay Lagged\" -eq $true) -or ($_.\"Truncation Lagged\" -eq $true) }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Lagged Queues\" -Value $laggedqueues -Force\n\n            $healthyindexes = @($copies | Where-Object { ($_.\"Content Index\" -eq \"Healthy\" -or $_.\"Content Index\" -eq \"Disabled\" -or $_.\"Content Index\" -eq \"AutoSuspended\") }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Healthy Indexes\" -Value $healthyindexes -Force\n            \n            $unhealthyindexes = @($copies | Where-Object { ($_.\"Content Index\" -ne \"Healthy\" -and $_.\"Content Index\" -ne \"Disabled\" -and $_.\"Content Index\" -ne \"AutoSuspended\") }).Count\n            $databaseObj | Add-Member NoteProperty -Name \"Unhealthy Indexes\" -Value $unhealthyindexes -Force\n            \n            $dagdatabaseSummary += $databaseObj\n        \n        }\n        \n        #Get Test-Replication Health results for each DAG member\n        foreach ($dagmember in $dagmembers)\n        {\n            $replicationhealth = $null\n\n            $replicationhealthitems = @{\n                                        ClusterService = $null\n                                        ReplayService = $null\n                                        ActiveManager = $null\n                                        TasksRpcListener = $null\n                                        TcpListener = $null\n                                        ServerLocatorService = $null\n                                        DagMembersUp = $null\n                                        ClusterNetwork = $null\n                                        QuorumGroup = $null\n                                        FileShareQuorum = $null\n                                        DatabaseRedundancy = $null\n                                        DatabaseAvailability = $null\n                                        DBCopySuspended = $null\n                                        DBCopyFailed = $null\n                                        DBInitializing = $null\n                                        DBDisconnected = $null\n                                        DBLogCopyKeepingUp = $null\n                                        DBLogReplayKeepingUp = $null\n                                        }\n\n            $memberObj = New-Object PSObject -Property $replicationhealthitems\n            $memberObj | Add-Member NoteProperty -Name \"Server\" -Value $($dagmember.Name)\n        \n            $tmpstring = \"---- Checking replication health for $($dagmember.Name)\"\n            Write-Verbose $tmpstring\n            if ($Log) {Write-Logfile $tmpstring}\n            \n            if ($HasE15)\n            {\n                $DagMemberVer = ($GetExchangeServerResults | Where-Object {$_.Name -ieq $dagmember.Name}).AdminDisplayVersion.ToString()\n            }\n            \n\n            if ($DagMemberVer -like \"Version 14.*\")\n            {\n                if ($Log) {Write-Logfile \"Using E14 replication health test workaround\"}\n                $replicationhealth = Test-E14ReplicationHealth $dagmember\n            }\n            else\n            {\n                $replicationhealth = Test-ReplicationHealth -Identity $dagmember\n            }\n            \n            foreach ($healthitem in $replicationhealth)\n            {\n                if ($($healthitem.Result) -eq $null)\n                {\n                    $healthitemresult = \"n\/a\"\n                }\n                else\n                {\n                    $healthitemresult = $($healthitem.Result)\n                }\n                $tmpstring = \"$($healthitem.Check) $healthitemresult\"\n                Write-Verbose $tmpstring\n                if ($Log) {Write-Logfile $tmpstring}\n                $memberObj | Add-Member NoteProperty -Name $($healthitem.Check) -Value $healthitemresult -Force\n            }\n            $dagmemberReport += $memberObj\n        }\n\n        \n        #Generate the HTML from the DAG health checks\n        if ($SendEmail -or $ReportFile)\n        {\n        \n            ####Begin Summary Table HTML\n            $dagdatabaseSummaryHtml = $null\n            #Begin Summary table HTML header\n            $htmltableheader = \"&lt;p>\n                            &lt;table>\n                            &lt;tr>\n                            &lt;th>Database&lt;\/th>\n                            &lt;th>Mounted on&lt;\/th>\n                            &lt;th>Preference&lt;\/th>\n                            &lt;th>Total Copies&lt;\/th>\n                            &lt;th>Healthy Copies&lt;\/th>\n                            &lt;th>Unhealthy Copies&lt;\/th>\n                            &lt;th>Healthy Queues&lt;\/th>\n                            &lt;th>Unhealthy Queues&lt;\/th>\n                            &lt;th>Lagged Queues&lt;\/th>\n                            &lt;th>Healthy Indexes&lt;\/th>\n                            &lt;th>Unhealthy Indexes&lt;\/th>\n                            &lt;\/tr>\"\n\n            $dagdatabaseSummaryHtml += $htmltableheader\n            #End Summary table HTML header\n            \n            #Begin Summary table HTML rows\n            foreach ($line in $dagdatabaseSummary)\n            {\n                $htmltablerow = \"&lt;tr>\"\n                $htmltablerow += \"&lt;td>&lt;strong>$($line.Database)&lt;\/strong>&lt;\/td>\"\n                \n                #Warn if mounted server is still unknown\n                switch ($($line.\"Mounted on\"))\n                {\n                    \"Unknown\" {\n                        $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Mounted on\")&lt;\/td>\"\n                        $dagsummary += \"$($line.Database) - $string61\"\n                        }\n                    default { $htmltablerow += \"&lt;td>$($line.\"Mounted on\")&lt;\/td>\" }\n                }\n                \n                #Warn if DB is mounted on a server that is not Activation Preference 1\n                if ($($line.Preference) -gt 1)\n                {\n                    $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.Preference)&lt;\/td>\"\n                    $dagsummary += \"$($line.Database) - $string62 $($line.Preference)\"\n                }\n                else\n                {\n                    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.Preference)&lt;\/td>\"\n                }\n                \n                $htmltablerow += \"&lt;td>$($line.\"Total Copies\")&lt;\/td>\"\n                \n                #Show as info if health copies is 1 but total copies also 1,\n                #Warn if healthy copies is 1, Fail if 0\n                switch ($($line.\"Healthy Copies\"))\n                {    \n                    0 {$htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Healthy Copies\")&lt;\/td>\"}\n                    1 {\n                        if ($($line.\"Total Copies\") -eq $($line.\"Healthy Copies\"))\n                        {\n                            $htmltablerow += \"&lt;td class=\"\"info\"\">$($line.\"Healthy Copies\")&lt;\/td>\"\n                        }\n                        else\n                        {\n                            $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Healthy Copies\")&lt;\/td>\"\n                        }\n                      }\n                    default {$htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Healthy Copies\")&lt;\/td>\"}\n                }\n\n                #Warn if unhealthy copies is 1, fail if more than 1\n                switch ($($line.\"Unhealthy Copies\"))\n                {\n                    0 {    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Unhealthy Copies\")&lt;\/td>\" }\n                    1 {\n                        $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Unhealthy Copies\")&lt;\/td>\"\n                        $dagsummary += \"$($line.Database) - $string63 $($line.\"Unhealthy Copies\") $string65 $($line.\"Total Copies\") $string66\"\n                        }\n                    default {\n                        $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Unhealthy Copies\")&lt;\/td>\"\n                        $dagsummary += \"$($line.Database) - $string63 $($line.\"Unhealthy Copies\") $string65 $($line.\"Total Copies\") $string66\"\n                        }\n                }\n\n                #Warn if healthy queues + lagged queues is less than total copies\n                #Fail if no healthy queues\n                if ($($line.\"Total Copies\") -eq ($($line.\"Healthy Queues\") + $($line.\"Lagged Queues\")))\n                {\n                    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Healthy Queues\")&lt;\/td>\"\n                }\n                else\n                {\n                    $dagsummary += \"$($line.Database) - $string64 $($line.\"Healthy Queues\") $string65 $($line.\"Total Copies\") $string66\"\n                    switch ($($line.\"Healthy Queues\"))\n                    {\n                        0 {    $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Healthy Queues\")&lt;\/td>\" }\n                        default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Healthy Queues\")&lt;\/td>\" }\n                    }\n                }\n                \n                #Fail if unhealthy queues = total queues\n                #Warn if more than one unhealthy queue\n                if ($($line.\"Total Queues\") -eq $($line.\"Unhealthy Queues\"))\n                {\n                    $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Unhealthy Queues\")&lt;\/td>\"\n                }\n                else\n                {\n                    switch ($($line.\"Unhealthy Queues\"))\n                    {\n                        0 { $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Unhealthy Queues\")&lt;\/td>\" }\n                        default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Unhealthy Queues\")&lt;\/td>\" }\n                    }\n                }\n                \n                #Info for lagged queues\n                switch ($($line.\"Lagged Queues\"))\n                {\n                    0 { $htmltablerow += \"&lt;td>$($line.\"Lagged Queues\")&lt;\/td>\" }\n                    default { $htmltablerow += \"&lt;td class=\"\"info\"\">$($line.\"Lagged Queues\")&lt;\/td>\" }\n                }\n                \n                #Pass if healthy indexes = total copies\n                #Warn if healthy indexes less than total copies\n                #Fail if healthy indexes = 0\n                if ($($line.\"Total Copies\") -eq $($line.\"Healthy Indexes\"))\n                {\n                    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Healthy Indexes\")&lt;\/td>\"\n                }\n                else\n                {\n                    $dagsummary += \"$($line.Database) - $string67 $($line.\"Unhealthy Indexes\") $string65 $($line.\"Total Copies\") $string66\"\n                    switch ($($line.\"Healthy Indexes\"))\n                    {\n                        0 { $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Healthy Indexes\")&lt;\/td>\" }\n                        default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Healthy Indexes\")&lt;\/td>\" }\n                    }\n                }\n                \n                #Fail if unhealthy indexes = total copies\n                #Warn if unhealthy indexes 1 or more\n                #Pass if unhealthy indexes = 0\n                if ($($line.\"Total Copies\") -eq $($line.\"Unhealthy Indexes\"))\n                {\n                    $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Unhealthy Indexes\")&lt;\/td>\"\n                }\n                else\n                {\n                    switch ($($line.\"Unhealthy Indexes\"))\n                    {\n                        0 { $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Unhealthy Indexes\")&lt;\/td>\" }\n                        default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Unhealthy Indexes\")&lt;\/td>\" }\n                    }\n                }\n                \n                $htmltablerow += \"&lt;\/tr>\"\n                $dagdatabaseSummaryHtml += $htmltablerow\n            }\n            $dagdatabaseSummaryHtml += \"&lt;\/table>\n                                    &lt;\/p>\"\n            #End Summary table HTML rows\n            ####End Summary Table HTML\n\n            ####Begin Detail Table HTML\n            $databasedetailsHtml = $null\n            #Begin Detail table HTML header\n            $htmltableheader = \"&lt;p>\n                                &lt;table>\n                                &lt;tr>\n                                &lt;th>Database Copy&lt;\/th>\n                                &lt;th>Database Name&lt;\/th>\n                                &lt;th>Mailbox Server&lt;\/th>\n                                &lt;th>Activation Preference&lt;\/th>\n                                &lt;th>Status&lt;\/th>\n                                &lt;th>Copy Queue&lt;\/th>\n                                &lt;th>Replay Queue&lt;\/th>\n                                &lt;th>Replay Lagged&lt;\/th>\n                                &lt;th>Truncation Lagged&lt;\/th>\n                                &lt;th>Content Index&lt;\/th>\n                                &lt;\/tr>\"\n\n            $databasedetailsHtml += $htmltableheader\n            #End Detail table HTML header\n            \n            #Begin Detail table HTML rows\n            foreach ($line in $dagdbcopyReport)\n            {\n                $htmltablerow = \"&lt;tr>\"\n                $htmltablerow += \"&lt;td>&lt;strong>$($line.\"Database Copy\")&lt;\/strong>&lt;\/td>\"\n                $htmltablerow += \"&lt;td>$($line.\"Database Name\")&lt;\/td>\"\n                $htmltablerow += \"&lt;td>$($line.\"Mailbox Server\")&lt;\/td>\"\n                $htmltablerow += \"&lt;td>$($line.\"Activation Preference\")&lt;\/td>\"\n                \n                Switch ($($line.\"Status\"))\n                {\n                    \"Healthy\" { $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Status\")&lt;\/td>\" }\n                    \"Mounted\" { $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Status\")&lt;\/td>\" }\n                    \"Failed\" { $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Status\")&lt;\/td>\" }\n                    \"FailedAndSuspended\" { $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Status\")&lt;\/td>\" }\n                    \"ServiceDown\" { $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Status\")&lt;\/td>\" }\n                    \"Dismounted\" { $htmltablerow += \"&lt;td class=\"\"fail\"\">$($line.\"Status\")&lt;\/td>\" }\n                    default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Status\")&lt;\/td>\" }\n                }\n                \n                if ($($line.\"Copy Queue\") -lt $replqueuewarning)\n                {\n                    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Copy Queue\")&lt;\/td>\"\n                }\n                else\n                {\n                    $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Copy Queue\")&lt;\/td>\"\n                }\n                \n                if (($($line.\"Replay Queue\") -lt $replqueuewarning) -or ($($line.\"Replay Lagged\") -eq $true))\n                {\n                    $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Replay Queue\")&lt;\/td>\"\n                }\n                else\n                {\n                    $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Replay Queue\")&lt;\/td>\"\n                }\n                \n\n                Switch ($($line.\"Replay Lagged\"))\n                {\n                    $true { $htmltablerow += \"&lt;td class=\"\"info\"\">$($line.\"Replay Lagged\")&lt;\/td>\" }\n                    default { $htmltablerow += \"&lt;td>$($line.\"Replay Lagged\")&lt;\/td>\" }\n                }\n\n                Switch ($($line.\"Truncation Lagged\"))\n                {\n                    $true { $htmltablerow += \"&lt;td class=\"\"info\"\">$($line.\"Truncation Lagged\")&lt;\/td>\" }\n                    default { $htmltablerow += \"&lt;td>$($line.\"Truncation Lagged\")&lt;\/td>\" }\n                }\n                \n                Switch ($($line.\"Content Index\"))\n                {\n                    \"Healthy\" { $htmltablerow += \"&lt;td class=\"\"pass\"\">$($line.\"Content Index\")&lt;\/td>\" }\n                    \"Disabled\" { $htmltablerow += \"&lt;td class=\"\"info\"\">$($line.\"Content Index\")&lt;\/td>\" }\n                    default { $htmltablerow += \"&lt;td class=\"\"warn\"\">$($line.\"Content Index\")&lt;\/td>\" }\n                }\n                \n                $htmltablerow += \"&lt;\/tr>\"\n                $databasedetailsHtml += $htmltablerow\n            }\n            $databasedetailsHtml += \"&lt;\/table>\n                                    &lt;\/p>\"\n            #End Detail table HTML rows\n            ####End Detail Table HTML\n            \n            \n            ####Begin Member Table HTML\n            $dagmemberHtml = $null\n            #Begin Member table HTML header\n            $htmltableheader = \"&lt;p>\n                                &lt;table>\n                                &lt;tr>\n                                &lt;th>Server&lt;\/th>\n                                &lt;th>Cluster Service&lt;\/th>\n                                &lt;th>Replay Service&lt;\/th>\n                                &lt;th>Active Manager&lt;\/th>\n                                &lt;th>Tasks RPC Listener&lt;\/th>\n                                &lt;th>TCP Listener&lt;\/th>\n                                &lt;th>Server Locator Service&lt;\/th>\n                                &lt;th>DAG Members Up&lt;\/th>\n                                &lt;th>Cluster Network&lt;\/th>\n                                &lt;th>Quorum Group&lt;\/th>\n                                &lt;th>File Share Quorum&lt;\/th>\n                                &lt;th>Database Redundancy&lt;\/th>\n                                &lt;th>Database Availability&lt;\/th>\n                                &lt;th>DB Copy Suspended&lt;\/th>\n                                &lt;th>DB Copy Failed&lt;\/th>\n                                &lt;th>DB Initializing&lt;\/th>\n                                &lt;th>DB Disconnected&lt;\/th>\n                                &lt;th>DB Log Copy Keeping Up&lt;\/th>\n                                &lt;th>DB Log Replay Keeping Up&lt;\/th>\n                                &lt;\/tr>\"\n            \n            $dagmemberHtml += $htmltableheader\n            #End Member table HTML header\n            \n            #Begin Member table HTML rows\n            foreach ($line in $dagmemberReport)\n            {\n                $htmltablerow = \"&lt;tr>\"\n                $htmltablerow += \"&lt;td>&lt;strong>$($line.\"Server\")&lt;\/strong>&lt;\/td>\"\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"ClusterService\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"ReplayService\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"ActiveManager\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"TasksRPCListener\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"TCPListener\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"ServerLocatorService\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DAGMembersUp\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"ClusterNetwork\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"QuorumGroup\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"FileShareQuorum\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DatabaseRedundancy\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DatabaseAvailability\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBCopySuspended\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBCopyFailed\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBInitializing\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBDisconnected\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBLogCopyKeepingUp\")\n                $htmltablerow += (New-DAGMemberHTMLTableCell \"DBLogReplayKeepingUp\")\n                $htmltablerow += \"&lt;\/tr>\"\n                $dagmemberHtml += $htmltablerow\n            }\n            $dagmemberHtml += \"&lt;\/table>\n            &lt;\/p>\"\n        }\n        \n        #Output the report objects to console, and optionally to email and HTML file\n        #Forcing table format for console output due to issue with multiple output\n        #objects that have different layouts\n\n        #Write-Host \"---- Database Copy Health Summary ----\"\n        #$dagdatabaseSummary | ft\n                \n        #Write-Host \"---- Database Copy Health Details ----\"\n        #$dagdbcopyReport | ft\n        \n        #Write-Host \"`r`n---- Server Test-Replication Report ----`r`n\"\n        #$dagmemberReport | ft\n        \n        if ($SendEmail -or $ReportFile)\n        {\n            $dagreporthtml = $dagsummaryintro + $dagdatabaseSummaryHtml + $dagdetailintro + $databasedetailsHtml + $dagmemberintro + $dagmemberHtml\n            $dagreportbody += $dagreporthtml\n        }\n        \n    }\n}\nelse\n{\n    $tmpstring = \"No DAGs found\"\n    if ($Log) {Write-LogFile $tmpstring}\n    Write-Verbose $tmpstring\n    $dagreporthtml = \"&lt;p>No database availability groups found.&lt;\/p>\"\n}\n### End DAG Health Report\n\nWrite-Host $string16\n### Begin report generation\nif ($ReportMode -or $SendEmail)\n{\n    #Get report generation timestamp\n    $reportime = Get-Date\n\n    #Create HTML Report\n    #Common HTML head and styles\n    $htmlhead=\"&lt;html>\n                &lt;style>\n                BODY{font-family: Arial; font-size: 8pt;}\n                H1{font-size: 16px;}\n                H2{font-size: 14px;}\n                H3{font-size: 12px;}\n                TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}\n                TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}\n                TD{border: 1px solid black; padding: 5px; }\n                td.pass{background: #7FFF00;}\n                td.warn{background: #FFE600;}\n                td.fail{background: #FF0000; color: #ffffff;}\n                td.info{background: #85D4FF;}\n                &lt;\/style>\n                &lt;body>\n                &lt;h1 align=\"\"center\"\">Exchange Server Health Check Report&lt;\/h1>\n                &lt;h3 align=\"\"center\"\">Generated: $reportime&lt;\/h3>\"\n\n    #Check if the server summary has 1 or more entries\n    if ($($serversummary.count) -gt 0)\n    {\n        #Set alert flag to true\n        $alerts = $true\n    \n        #Generate the HTML\n        $serversummaryhtml = \"&lt;h3>Exchange Server Health Check Summary&lt;\/h3>\n                        &lt;p>The following server errors and warnings were detected.&lt;\/p>\n                        &lt;p>\n                        &lt;ul>\"\n        foreach ($reportline in $serversummary)\n        {\n            $serversummaryhtml +=\"&lt;li>$reportline&lt;\/li>\"\n        }\n        $serversummaryhtml += \"&lt;\/ul>&lt;\/p>\"\n        $alerts = $true\n    }\n    else\n    {\n        #Generate the HTML to show no alerts\n        $serversummaryhtml = \"&lt;h3>Exchange Server Health Check Summary&lt;\/h3>\n                        &lt;p>No Exchange server health errors or warnings.&lt;\/p>\"\n    }\n    \n    #Check if the DAG summary has 1 or more entries\n    if ($($dagsummary.count) -gt 0)\n    {\n        #Set alert flag to true\n        $alerts = $true\n    \n        #Generate the HTML\n        $dagsummaryhtml = \"&lt;h3>Database Availability Group Health Check Summary&lt;\/h3>\n                        &lt;p>The following DAG errors and warnings were detected.&lt;\/p>\n                        &lt;p>\n                        &lt;ul>\"\n        foreach ($reportline in $dagsummary)\n        {\n            $dagsummaryhtml +=\"&lt;li>$reportline&lt;\/li>\"\n        }\n        $dagsummaryhtml += \"&lt;\/ul>&lt;\/p>\"\n        $alerts = $true\n    }\n    else\n    {\n        #Generate the HTML to show no alerts\n        $dagsummaryhtml = \"&lt;h3>Database Availability Group Health Check Summary&lt;\/h3>\n                        &lt;p>No Exchange DAG errors or warnings.&lt;\/p>\"\n    }\n\n\n    #Exchange Server Health Report Table Header\n    $htmltableheader = \"&lt;h3>Exchange Server Health&lt;\/h3>\n                        &lt;p>\n                        &lt;table>\n                        &lt;tr>\n                        &lt;th>Server&lt;\/th>\n                        &lt;th>Site&lt;\/th>\n                        &lt;th>Roles&lt;\/th>\n                        &lt;th>Version&lt;\/th>\n                        &lt;th>DNS&lt;\/th>\n                        &lt;th>Ping&lt;\/th>\n                        &lt;th>Uptime (hrs)&lt;\/th>\n                        &lt;th>Client Access Server Role Services&lt;\/th>\n                        &lt;th>Hub Transport Server Role Services&lt;\/th>\n                        &lt;th>Mailbox Server Role Services&lt;\/th>\n                        &lt;th>Unified Messaging Server Role Services&lt;\/th>\n                        &lt;th>Transport Queue&lt;\/th>\n                        &lt;th>PF DBs Mounted&lt;\/th>\n                        &lt;th>MB DBs Mounted&lt;\/th>\n                        &lt;th>MAPI Test&lt;\/th>\n                        &lt;th>Mail Flow Test&lt;\/th>\n                        &lt;\/tr>\"\n\n    #Exchange Server Health Report Table\n    $serverhealthhtmltable = $serverhealthhtmltable + $htmltableheader                    \n                        \n    foreach ($reportline in $report)\n    {\n        $htmltablerow = \"&lt;tr>\"\n        $htmltablerow += \"&lt;td>$($reportline.server)&lt;\/td>\"\n        $htmltablerow += \"&lt;td>$($reportline.site)&lt;\/td>\"\n        $htmltablerow += \"&lt;td>$($reportline.roles)&lt;\/td>\"\n        $htmltablerow += \"&lt;td>$($reportline.version)&lt;\/td>\"                    \n        $htmltablerow += (New-ServerHealthHTMLTableCell \"dns\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"ping\")\n        \n        if ($($reportline.\"uptime (hrs)\") -eq \"Access Denied\")\n        {\n            $htmltablerow += \"&lt;td class=\"\"warn\"\">Access Denied&lt;\/td>\"        \n        }\n        elseif ($($reportline.\"uptime (hrs)\") -eq $string17)\n        {\n            $htmltablerow += \"&lt;td class=\"\"warn\"\">$string17&lt;\/td>\"\n        }\n        else\n        {\n            $hours = [int]$($reportline.\"uptime (hrs)\")\n            if ($hours -le 24)\n            {\n                $htmltablerow += \"&lt;td class=\"\"warn\"\">$hours&lt;\/td>\"\n            }\n            else\n            {\n                $htmltablerow += \"&lt;td class=\"\"pass\"\">$hours&lt;\/td>\"\n            }\n        }\n\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"Client Access Server Role Services\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"Hub Transport Server Role Services\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"Mailbox Server Role Services\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"Unified Messaging Server Role Services\")\n        #$htmltablerow += (New-ServerHealthHTMLTableCell \"Transport Queue\")\n        if ($($reportline.\"Transport Queue\") -match \"Pass\")\n        {\n            $htmltablerow += \"&lt;td class=\"\"pass\"\">$($reportline.\"Transport Queue\")&lt;\/td>\"\n        }\n        elseif ($($reportline.\"Transport Queue\") -match \"Warn\")\n        {\n            $htmltablerow += \"&lt;td class=\"\"warn\"\">$($reportline.\"Transport Queue\")&lt;\/td>\"\n        }\n        elseif ($($reportline.\"Transport Queue\") -match \"Fail\")\n        {\n            $htmltablerow += \"&lt;td class=\"\"fail\"\">$($reportline.\"Transport Queue\")&lt;\/td>\"\n        }\n        elseif ($($reportline.\"Transport Queue\") -eq \"n\/a\")\n        {\n            $htmltablerow += \"&lt;td>$($reportline.\"Transport Queue\")&lt;\/td>\"\n        }\n        else\n        {\n            $htmltablerow += \"&lt;td class=\"\"warn\"\">$($reportline.\"Transport Queue\")&lt;\/td>\"\n        }\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"PF DBs Mounted\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"MB DBs Mounted\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"MAPI Test\")\n        $htmltablerow += (New-ServerHealthHTMLTableCell \"Mail Flow Test\")\n        $htmltablerow += \"&lt;\/tr>\"\n        \n        $serverhealthhtmltable = $serverhealthhtmltable + $htmltablerow\n    }\n\n    $serverhealthhtmltable = $serverhealthhtmltable + \"&lt;\/table>&lt;\/p>\"\n\n    $htmltail = \"&lt;\/body>\n                &lt;\/html>\"\n\n    $htmlreport = $htmlhead + $serversummaryhtml + $dagsummaryhtml + $serverhealthhtmltable + $dagreportbody + $htmltail\n    \n    if ($ReportMode -or $ReportFile)\n    {\n        $htmlreport | Out-File $ReportFile -Encoding UTF8\n    }\n\n    if ($SendEmail)\n    {\n        if ($alerts -eq $false -and $AlertsOnly -eq $true)\n        {\n            #Do not send email message\n            Write-Host $string19\n            if ($Log) {Write-Logfile $string19}\n        }\n        else\n        {\n            #Send email message\n            Write-Host $string14\n            Send-MailMessage @smtpsettings -Body $htmlreport -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8)\n        }\n    }\n}\n### End report generation\n\n\nWrite-Host $string15\nif ($Log) {Write-Logfile $string15}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[57,88,89],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-exchange","category-powershell","tag-exchange","tag-test","tag-testing"],"_links":{"self":[{"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/comments?post=269"}],"version-history":[{"count":3,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":288,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/posts\/269\/revisions\/288"}],"wp:attachment":[{"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thelamberts.com\/wiki\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}