Thursday, 26 December 2024

SFB Health Check

[array]$AllUserPools    = @(Get-CsService -UserServer | Select-Object -ExpandProperty PoolFqdn)
[array]$AllEdgePools    = @(Get-CsService -EdgeServer | Select-Object -ExpandProperty PoolFqdn)
[array]$AllFileStores   = @(Get-CsService -FileStore | Select-Object -ExpandProperty UncPath)
[array]$AllSQLServers   = @(Get-CsService -ApplicationDatabase | Select-Object -ExpandProperty PoolFqdn)
[array]$AllSFBServers  = @()
[array]$AllEdgeServers  = @()
# Dynamically builds list of all SfB Servers based on pools
foreach ($pool in $AllUserPools)
{
    $AllSFBServers += Get-CsComputer -Pool $pool | Select-Object -ExpandProperty Fqdn
}
$AllSfBServers  += "Server01"
$AllSfBServers  += "Server02"
$AllSfBServers  += "Server03"
$AllSfBServers  += "Server04"
$AllSfBServers   = $AllSfBServers | Sort-Object
# Dynamically builds list of Edge servers based on Edge pools
foreach ($pool in $AllEdgePools)
{
    $AllEdgeServers += Get-CsComputer -Pool $pool | Select-Object -ExpandProperty Fqdn
}
# Sorts $AllEdgeServers by Name
$AllEdgeServers = $AllEdgeServers | Sort-Object
#endregion


###################################### Checking Services
 
$AllServerServiceStatus = [system.collections.arraylist]@()
$AllServerServiceStatus.Clear()
foreach ($server in $AllSfBServers)

    Write-Host "$server" -ForegroundColor Yellow
    #$CurrentServiceStatus = Get-CsWindowsService -ComputerName $server | select-object Name, Status
    $ServiceStatus = Get-CsWindowsService -ComputerName $server
    $(if(($ServiceStatus|Where-Object{$_.status -ne "Running"}).Count -gt 0){
        $RunningStatus = "Not Running"
    }
    else{
        $RunningStatus = "Running"
    })
    $ServiceProperty=[pscustomobject][ordered]@{
            Server = $server
            Status = $RunningStatus
    }
    $AllServerServiceStatus.Add($ServiceProperty)
}
$AllServerServiceStatus 


###################################### Testing if SQL Servers respond to connectivity test
$SQLServerPINGStatus = [system.collections.arraylist]@()
$SQLServerPINGStatus.Clear()
foreach ($sql in $AllSQLServers)
{    
    if ((Test-Connection -ComputerName $sql -Count 2 -Quiet) -eq $true)
    {
       $PingStatus = [pscustomobject][ordered]@{
            Server = $Sql
            Status = "Healthy"
       }
       $SQLServerPINGStatus.Add($PingStatus)
    }
    else
    {
       $PingStatus = [pscustomobject][ordered]@{
            Server = $Sql
            Status = "Unhealthy"
       }
       $SQLServerPINGStatus.Add($PingStatus)
    }
}
$SQLServerPINGStatus

###################################### Checks availability of each pool's file share
$PoolFileShareAvailability = [system.collections.arraylist]@()
$PoolFileShareAvailability.Clear()
foreach ($store in $AllFileStores)
{
    $test = Test-Path -Path $store
    if ((Test-Path -Path $store) -eq $true){
        $AvailabilityStatus = [pscustomobject][ordered]@{
            FileShare = $store
            Status = "Healthy"
        }
        $PoolFileShareAvailability.Add($AvailabilityStatus)
    }
    else{
       $AvailabilityStatus = [pscustomobject][ordered]@{
            FileShare = $store
            Status = "Unhealthy"
       }
       $PoolFileShareAvailability.Add($AvailabilityStatus)
    }
}
$PoolFileShareAvailability

###################################### Checks all servers for up to topology replication
$ReplicationService = [system.collections.arraylist]@()
$ReplicationService.Clear()
$replServers = Get-CsManagementStoreReplicationStatus | Sort-Object ReplicaFqdn
foreach ($replServer in $replServers)
{              
    if ($replServer.UpToDate -eq "True"){
        $ReplicationServiceStatus = [pscustomobject][ordered]@{
            Server = $replServer.ReplicaFqdn
            UpToDate = "True"
            LastStatusReport = $replServer.LastStatusReport
            Status = "Healthy"
        }
        $ReplicationService.Add($ReplicationServiceStatus)
    }
    else
    {
        $ReplicationServiceStatus = [pscustomobject][ordered]@{
            Server = $replServer.ReplicaFqdn
            UpToDate = "False"
            LastStatusReport = $replServer.LastStatusReport
            Status = "UnHealthy"
        }
        $ReplicationService.Add($ReplicationServiceStatus)
    }

$ReplicationService

###################################### Get Active User Count
$ActiveUserCount = [system.collections.arraylist]@()
$ActiveUserCount.Clear()
$UserCount = get-csuser -ResultSize unlimited | group-object -Property registrarpool | Select-Object name,Count |Sort-Object -Property count
@($UserCount | select -SkipLast 1).ForEach({
    $Property = [pscustomobject][ordered]@{
        Name = $_.Name
        Count = $_.Count
    }
    $ActiveUserCount.Add($Property)
})
$Val = $UserCount | select -Last 1| Select @{n="Name";e={'Microsoft Teams'}},@{n="Count";e={$_.Count}}
$ActiveUserCount.Add($Val)
$ActiveUserCount

###################################### SFB Server Disk Size Report
$DiskSize = [system.collections.arraylist]@()
$DiskSize.Clear()
$AllSfBServers = $AllSfBServers |Where-Object{$_ -notmatch "CMS001"}
foreach($server in $AllSfBServers){
    $Size = Get-WmiObject Win32_LogicalDisk -ComputerName $server | where{$_.DriveType -eq '3'} `
    | Select DeviceID, DriveType,VolumeName,@{N='TotalSize(GB)';E={[Math]::Ceiling($_.Size/1GB)}}, `
    @{N='FreeSize(GB)';E={[Math]::Ceiling($_.FreeSpace/1GB)}}
    #$Size = $Size | Where-Object{$_.DeviceId -eq "c:"}
    @($Size).ForEach({
        $SizeDetails = [pscustomobject][ordered]@{
            Server = $server
            Drive = $_.DeviceID
            "TotalSize(GB)" = $_.'TotalSize(GB)'
            "FreeSize(GB)"  = $_.'FreeSize(GB)'
            "Available"       =[String]([Math]::Round(([Int]($_.'FreeSize(GB)') / [Int]($_.'TotalSize(GB)')) * 100)) + " %"
        }
        $DiskSize.Add($SizeDetails)
    })
    $SizeDetails = [pscustomobject][ordered]@{
            Server = ""
            Drive = ""
            "TotalSize(GB)" = ""
            "FreeSize(GB)"  = ""
            "Available"     = ""
    }
    $DiskSize.Add($SizeDetails)
}
$DiskSize | ft

$head = @"
<style>
h1, h5, th { text-align: center; }
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
</style>
"@

if(($AllServerServiceStatus.Status -ne "Running").Count -gt 0){$Report_AllServerServiceStatus = "Fail"}
else{$Report_AllServerServiceStatus = "Pass"}
if(($SQLServerPINGStatus.Status -ne "Healthy").Count -gt 0){$Report_SQLServerPINGStatus = "Fail"}
else{$Report_SQLServerPINGStatus = "Pass"}
if(($PoolFileShareAvailability.Status -ne "Healthy").Count -gt 0){$Report_PoolFileShareAvailability = "Fail"}
else{$Report_PoolFileShareAvailability = "Pass"}
if(($ReplicationService.Status -ne "Healthy").Count -gt 0){$Report_ReplicationService = "Fail"}
else{$Report_ReplicationService = "Pass"}
if($ActiveUserCount|Where-Object{($_.Name -notmatch "Teams") -and ($_.Count -gt 1500)}){
    $Report_ActiveUserCount = "Fail"
}
else{
    $Report_ActiveUserCount = "Pass"
}
if([int]($DiskSize | Where-Object{[int]((($_.Available -split '[" "]')[0]) -le 10) -and ($_.Available -ne "")}).Count -gt 0){
    $Report_DiskSize = "Fail"
}
else{$Report_DiskSize = "Pass"}
$Overview = [System.collections.arraylist]@()
$Overview.Clear()
$Properties = [pscustomobject][ordered]@{
    AllServerServiceStatus    = $Report_AllServerServiceStatus
    SQLServerPINGStatus       = $Report_SQLServerPINGStatus
    PoolFileShareAvailability = $Report_PoolFileShareAvailability
    ReplicationService        = $Report_ReplicationService
    ActiveUserCount           = $Report_ActiveUserCount
    DiskSize                  = $Report_DiskSize
}
$Overview.Add($Properties)
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;"><centre>SFB Overall Status Report</span></centre></h3>
"@
$SFBReport = $Overview | ConvertTo-Html -Body $body -Head $head

$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;"><centre>SFB Service Status</span></centre></h3>
"@
$SFBReport += $AllServerServiceStatus| ConvertTo-Html -Body $body -Head $head 
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;">SQL Server PING Status</span></h3>
"@
$SFBReport += $SQLServerPINGStatus| ConvertTo-Html -Body $body -Head $head 
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;">SFB PoolFileShareAvailability</span></h3>
"@
$SFBReport += $PoolFileShareAvailability| ConvertTo-Html -Body $body -Head $head 
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;">SFB Replication Status</span></h3>
"@
$SFBReport += $ReplicationService| ConvertTo-Html -Body $body -Head $head 
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;">SFB Total Active User Count</span></h3>
"@
$SFBReport +=$ActiveUserCount | ConvertTo-Html -Body $body -Head $head 
$body=@"
<h3><span style="color: rgb(235, 107, 86); font-family: Calibri, sans-serif; font-size: 20px;">SFB Disk Size Report</span></h3>
"@
$SFBReport += $DiskSize | ConvertTo-Html -Body $body -Head $head
#$SFBReport | Out-File E:\Software\Scripts\SUMANT.html
if($Overview -match "Fail"){
    $SubjectStatus = "Fail"
}
else{
    $SubjectStatus = "Pass"
}
$SMTPServer = "xxxxxxxxxxxx"
$SMTPPort = "25"
#Add email address of person in to & cc  whom you are sending email (Multiple addresses should be comma seperated)
$to = "User1@email.com,User2@email.com"
#$cc = "User3@email.com"
$subject = "$((Get-Date).GetDateTimeFormats()[0]) - Skype For Business - Daily Health Check - $($SubjectStatus)"
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $SFBReport
$message.to.add($to)
#$message.cc.add($cc)
$message.IsBodyHtml = $True
$message.from = "Automation@email.com"
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer,$SMTPPort);
$smtp.send($message)
Write-Host `t -NoNewline
Write-Host "Sending Email...: " -ForegroundColor White -NoNewline
Write-Host "Email Sent" -ForegroundColor Green



---------------------------------- End ---------------------------------- 

No comments:

Post a Comment