Last active
March 27, 2018 23:57
-
-
Save tostka/c601387ae0aae48d8aea065d97b2f3cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#check-ExchSvcStatusRpt.ps1 | |
<# This gist is trimmed to just the core data gathering bits... | |
#> | |
#------------------------------------------------- | |
# do data collection here | |
<# trimmed #> | |
# Ex svcs | |
$svcs = (get-service -include *exchange* -computername $env:COMPUTERNAME | ?{$_.name -notmatch '^(Mimosa.*|wsbexchange)$'} | select name,DisplayName,status ); | |
$svcs | Format-Table -AutoSize | |
#*======v HTML-EMAIL-REPORT-ASSY-BOILERPLATE v====== | |
# 1:08 PM 12/15/2015 build html rpt fragment: | |
# 9:16 AM 12/16/2015 If aggregating, we can just genericize the varis for this | |
# collect data object into stock vari & assign a caption for this chunk of report | |
# use | out-string on the inbound object, if getting object references rather than text | |
$RptFragData = $svcs #| out-string; | |
$RptFragCaption = "Exchange Service Health on $ComputerName" ; | |
# note: -As LIST creates a vertical report; -As TABLE creates a horizontal report; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
# append the fragment to the $RptContent aggregator | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
#*======^ HTML-EMAIL-REPORT-ASSY-BOILERPLATE ^====== | |
if($ExServer.ismailboxserver){ | |
$DeadSvc = ($svcs | ?{($_.Status -ne 'Running') -AND ($_.NAME -notmatch $rgxExMbxNonRunSvc)} ); | |
} else { | |
# search is only supposed to be running on mbx | |
$DeadSvc = ($svcs | ?{($_.Status -ne 'Running') -AND ($_.NAME -notmatch $rgxExHubNonRunSvc)} ); | |
} | |
if ($DeadSvc){ | |
$msg="STOPPED SVCS" ; | |
write-host -foregroundcolor green $msg; | |
$DeadSvc | ft Name,Status | |
<# trimmed #> | |
$RptFragData = $DeadSvc | select Name,Status #| out-string ; | |
$RptFragCaption = "STOPPED SVCS" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
# append the fragment to the $RptContent aggregator | |
# 1:35 PM 10/2/2017 add remedial svc start | |
$maxRepeat = 20 ; | |
foreach($svc in $DeadSvc){ | |
write-verbose -verbose:$true "$((get-date).ToString('HH:mm:ss')):Starting Svc:$($svc.Name) and waiting for Running|Stopped Status" ; | |
start-service -name $svc.Name -ea 0 ; | |
start-sleep -s 5 ; | |
Do { | |
write-host "." -NoNewLine; | |
$maxRepeat-- ; | |
Start-Sleep -s 5 | |
} | |
Until ((get-service -name $svc.Name |?{$_.status -match '^(Running|Stopped)$'}) -OR ($maxRepeat -eq 0)) ; | |
} ; | |
# 2:06 PM 10/2/2017 re-pull curr status | |
$svcs = (get-service -include *exchange* -computername $env:COMPUTERNAME | ?{$_.name -notmatch '^(Mimosa.*|wsbexchange)$'} | select name,DisplayName,status ); | |
if($ExServer.ismailboxserver){ | |
$DeadSvc = ($svcs | ?{($_.Status -ne 'Running') -AND ($_.NAME -notmatch $rgxExMbxNonRunSvc)} ); | |
} else { | |
$DeadSvc = ($svcs | ?{($_.Status -ne 'Running') -AND ($_.NAME -notmatch $rgxExHubNonRunSvc)} ); | |
} ; | |
$bRet=$DeadSvc ; | |
<# trimmed #> | |
if ($bRet) { | |
$RptFragData = $bRet #| out-string ; | |
} else { $RptFragData = "(no un-remediated/stopped services present) " }; | |
$RptFragCaption = "Post-Remediated Start Status:" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
# append the fragment to the $RptContent aggregator | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
} else { | |
<# trimmed #> | |
$msg= "[no non-runnicng svcs (that should be running)]`n"; | |
write-host -foregroundcolor green $msg; | |
<# trimmed #> | |
$RptFragData = " " #| out-string ; | |
$RptFragCaption = "[no non-runnicng svcs (that should be running)]" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
} ; | |
$msg=("=" * 15) ; | |
write-host -foregroundcolor green $msg; | |
$msg= "$(Get-Date -Format "HH:mm:ss"):`n`Checking Test-ServiceHealth on $ComputerName `n"; | |
write-host -foregroundcolor green $msg; | |
$bRet=Test-ServiceHealth | |
$bRet ; | |
$RptFragData = $bRet | select Role,RequiredServicesRunning,@{n="ServicesRunning";e={[string]::join(", ",$_.ServicesRunning)}},@{n="ServicesNotRunning";e={[string]::join(", ",$_.ServicesNotRunning)}} | |
$RptFragCaption = "Test-ServiceHealth on $ComputerName" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
$msg=("=" * 15) ; | |
write-host -foregroundcolor green $msg; | |
if($ExServer.ismailboxserver){ | |
$msg="$(Get-Date -Format "HH:mm:ss"):`n`Checking Test-ReplicationHealth on $ComputerName `n" ; | |
write-host -foregroundcolor green $msg; | |
$bRet = Test-ReplicationHealth #| out-string ; | |
$bRet ; | |
$RptFragData = $bRet | SELECT Server,Check,Result,Error #| out-string ; | |
$RptFragCaption = "Test-ReplicationHealth on $ComputerName" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
$msg=("=" * 15) ; | |
$msg="$(Get-Date -Format "HH:mm:ss"):`n`Checking Get-MailboxDatabaseCopyStatus on $ComputerName `n" ; | |
write-host -foregroundcolor green $msg; | |
# 1:12 PM 12/16/2015 subset select | |
$bRet = Get-MailboxDatabaseCopyStatus | sort status,name | select Name,Status,CopyQueueLength,ReplayQueueLength,LastInspectedLogTime,ContentIndexState #| out-string ; | |
$bRet ; | |
$RptFragData = $bRet #| out-string ; | |
$RptFragCaption = "Get-MailboxDatabaseCopyStatus on $ComputerName" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
$msg=("=" * 15) ; | |
$msg="$(Get-Date -Format "HH:mm:ss"):`nChecking Mailstore Hub Delivery Queues (non-Ready)`n" ; | |
write-host -foregroundcolor green $msg; | |
switch -regex ($env:computername.tostring().substring(0,3)){ | |
"(SITEA|SITEB)" { | |
$hubs = get-transportserver |?{$_.name -match "^(SITEA|SITEB).*$"} ; | |
foreach($hub in $hubs){ | |
$bRet = get-queue -server $hub -ea 0 |?{($_.NextHopDomain.tolower() -match "^(?i:SITEmsXX[01]mail\d{2})$") -AND ($_.identity -match "^(SITEA|SITEB).*") } |?{$_.Status -ne 'Ready'} | |
} ; | |
} ; | |
"SITEC" { | |
$hubs = get-transportserver |?{$_.name -match "^SITEC.*$"} ; | |
foreach($hub in $hubs){ | |
$bRet = get-queue -server $hub -ea 0 |?{ ($_.NextHopDomain.tolower() -match "^(?i:SITECmsXX[01]mail\d{2})$") -AND ($_.identity -match "^SITEC.*") } |?{$_.Status -ne 'Ready'} ; | |
} ; | |
} | |
"SITED" { | |
$hubs = get-transportserver |?{$_.name -match "^SPB.*$"} ; | |
foreach($hub in $hubs){ | |
$bRet = get-queue -server $hub -ea 0 |?{ ($_.NextHopDomain.tolower() -match "^(?i:SITEDmsXX[01]mail\d{2})$") -AND ($_.identity -match "^SITED.*") } |?{$_.Status -ne 'Ready'} ; | |
} ; | |
} | |
} ; | |
$bRet ; | |
if ($bRet) { | |
$RptFragData = $bRet #| out-string ; | |
} else { $RptFragData = "(no non-Ready MailStore queues present) " } ; | |
$RptFragCaption = "MailStore non-'Ready' queues" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
# 2:20 PM 10/2/2017 run test-mapi on a random mbx per db | |
$DAGName=(Get-MailboxServer $env:computername).DatabaseAvailabilityGroup ; | |
$dbs = (Get-MailboxDatabase | where {$_.MasterServerOrAvailabilityGroup -eq $DAGName}) ; | |
$bRet=$null ; $Result=$null ; | |
# aggregator | |
$Output = @() | |
foreach ($db in $dbs) { | |
write-host -fore yellow $db.name ; | |
$mbx = get-mailbox -database $db | get-random ; | |
write-host -fore yellow ("Testing mbx:" + $mbx) ; | |
if($host.version.major -ge 3){ | |
$Hash=[ordered]@{Dummy = $null ; } ; | |
} else {$Hash = New-Object Collections.Specialized.OrderedDictionary ; } ; | |
If($Hash.Contains("Dummy")){$Hash.remove("Dummy")} ; | |
if($mbx) { | |
$Result=($mbx | Test-MAPIConnectivity | select Mailbox,Server,Database,Result,Latency,Error) ; | |
$Hash.Add("Mailbox",$($Result.Mailbox)) ; | |
$Hash.Add("MailboxServer",$($Result.Server)) ; | |
$Hash.Add("Database",$($Result.Database)) ; | |
$Hash.Add("Result",$($Result.Result)) ; | |
$Hash.Add("Latency",$($Result.Latency)) ; | |
$Hash.Add("Error",$($Result.Error)) ; | |
$Output += New-Object PSObject -Property $Hash ; | |
} else { | |
$Hash.Add("Mailbox","(empty db)") ; | |
$Hash.Add("MailboxServer",$($db.Server)) ; | |
$Hash.Add("Database",$($db.name)) ; | |
$Hash.Add("Result","(empty db)") ; | |
$Hash.Add("Latency",$null) ; | |
$Hash.Add("Error",$null) ; | |
$Output += New-Object PSObject -Property $Hash ; | |
write-host -foregroundcolor green "$((get-date).ToString('HH:mm:ss')):$(($Output|out-string).trim())" ; | |
#$($db.name):(empty db)" ; | |
} ; | |
(get-date).ToString("mm/dd/yyyy HH:mm:ss") ; | |
}; | |
$bret=$Output | select mailbox,mailboxserver,database,result,latency,error | sort mailboxserver,database # | format-table -auto | |
if ($bRet) { | |
$RptFragData = $bRet #| out-string ; | |
} else { $RptFragData = "(no non-Ready MailStore queues present) " } ; | |
$RptFragCaption = "Access-Confirm: Test-MAPIConnectivity a random mailbox in each db" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
} ; | |
if($ExServer.IsHubTransportServer){ | |
$msg=("=" * 15) ; | |
$msg="$(Get-Date -Format "HH:mm:ss"):`nChecking Queue Status on $ComputerName (non-Ready)`n" ; | |
write-host -foregroundcolor green $msg; | |
$bRet = get-queue -sortorder:-MessageCount |?{$_.status -notmatch '(Ready|Active)'} #| out-string ; | |
$bRet ; | |
if ($bRet) { | |
$RptFragData = $bRet #| out-string ; | |
} else { $RptFragData = "(no non-Ready queues present) " } ; | |
$RptFragCaption = "Queue Status on $ComputerName (non-'Ready' queues)" ; | |
$RptFragment = $RptFragData | ConvertTo-Html -AS TABLE -Fragment -PreContent "<h3>$RptFragCaption</h3>" | Out-String ; | |
if($RptContent.length){ $RptContent+=$RptFragment } else {$RptContent=$RptFragment} ; | |
} ; | |
$msg=("=" * 15) ; | |
write-host -foregroundcolor green $msg; | |
#endregion DataGather ; # ------- | |
# ... | |
<# TRIMMED REMAINEDER IN THIS GIST, SEE FULL GITHUB REPO POST AT: | |
check-ExchSvcStatusRpt.ps1 - https://github.com/tostka/PowerShell/blob/master/ExchangeScripts/check-ExchSvcStatusRpt.ps1 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment