Background
You have been tasked to write a script to check the health of your Windows Server clusters. Additionally, you are also required to email the cluster health report to your team.
Script
In order for the following script to work, FailoverClusters module must be imported to the sever that this script will run on. This can be achived by running the following command and it is a one-time task.
Import-Module FailoverClusters
param(
#parameter 1, cluster server list
[String]$clustersfile,
#parameter 2, email the report or not. Default is Yes.
[String]$emailtoteam="Yes"
)
#start of summary text
$summary = "############## Summary ##############`r`n"
#define log file
$logfile = "path to log file_$(get-date -f yyyyMMddhhmmss).txt"
$clusterlist = get-content $clustersfile
foreach ($cluster in $clusterlist) {
$errorflag = 0
Write-Host $cluster
$summary += $cluster
$cluster | Out-File $logfile -Append
if(Get-Cluster -Name $cluster -ErrorAction Continue) {
### cluster node
$clusternodes = Get-ClusterNode -Cluster $cluster
$clusternodes | ft | Out-File $logfile -Append
foreach ($clusternode in $clusternodes) {
if(($clusternode | select -ExpandProperty State) -ne "Up") {
$errorflag += 1 #if the status is not UP, then report error
}
}
### cluster resource
$clusterresources = Get-ClusterResource -Cluster $cluster
$clusterresources | ft | Out-File $logfile -Append
foreach ($clusterresource in $clusterresources) {
if(($clusterresource | select -ExpandProperty State) -ne "Online") {
$errorflag += 1 #if the status is not ONLINE, then report error
}
}
if($errorflag -ne 0) {
"ERROR: Check cluster`r`n" | Out-File $logfile -Append
Write-Host "Check cluster" -BackgroundColor Red -ForegroundColor White
Write-Host ""
$summary += " : Check cluster`r`n"
} else {
"OK`r`n" | Out-File $logfile -Append
Write-Host "OK" -BackgroundColor Green -ForegroundColor Black
Write-Host ""
$summary += " : OK`r`n"
}
"----------------------------------------------`r`n" | Out-File $logfile -Append
} else {
"`r`nERROR: Cannot connect to cluster, Please check manually`r`n" | Out-File $logfile -Append
"----------------------------------------------`r`n" | Out-File $logfile -Append
Write-Host "Cannot connect to cluster, Please check manually" -BackgroundColor Red -ForegroundColor White
Write-Host ""
$summary += " : Cannot connect to cluster, Please check manually`r`n"
Continue
}
}
$summary | Out-File $logfile -Append
function sendemail{
if($emailtoteam -eq "Yes") {
$smtpserver = "smtp server hostname"
$dateformat = Get-Date -Format M
$emailsubject = "Daily cluster check - $dateformat"
$to = "report recipient mailbox"
Send-MailMessage -To $to -From "sender address" -SmtpServer $smtpserver -Subject $emailsubject -Body $summary -Attachments $logfile -Cc "copied recipient mailbox if any"
}
}
sendemail
No comments:
Post a Comment