How to create daily reports with System Center Operations Manager 2012 and PowerShell - Part 3

This series of blogs aims to show administrators how to create daily reports summarising the health of Operations Manager using PowerShell. This should reduce the overheads required for daily health reviews of their system environments.

The third entry in the blog series explains how PowerShell can be used to extract information from Operations Manager to show the health state of agents and then output this onto an HTML page. Agent health is key to ensuring data is being received by Operations Manager to allow access to all Operations Manager options and functionality. Subsequent posts will build on this principle to allow more sophisticated system health reporting.

 

#Outputs header and formatting for agent health

$ReportOutput += "<br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h6>Agent Health</h6>"

 

#Gets all agents that have an agent health state that is not green

$ReportOutput += "<h5>Agent Managed Health States which are not Green</h5>"

$Count = Get-SCOMAgent | where {$_.HealthState -ne "Success"} | Measure-Object

if($Count.Count -gt 0) {

                        $ReportOutput += Get-SCOMAgent | where {$_.HealthState -ne "Success"} | select Name,HealthState | ConvertTo-HTML

} else {

$ReportOutput += "<p><h4>Agent Managed Health State is Green.</h4></p>"

}

 

#Outputs header and formatting for unresponsive grey agents

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h5>Unresponsive Grey Agents</h5>"

 

 

#Gets all agents that are not available and in an unresponsive grey state

$AgentMonitoringClass = Get-SCOMClass -name "Microsoft.SystemCenter.Agent"

$Count = Get-SCOMClass -name "Microsoft.SystemCenter.Agent" | where {$_.IsAvailable -eq $false} | Measure-Object

if($Count.Count -gt 0) {

                        $ReportOutput += Get-SCOMClassInstance -monitoringclass:$AgentMonitoringClass | where {$_.IsAvailable -eq $false} | select DisplayName | ConvertTo-HTML

} else {

$ReportOutput += "<p><h4>All Agents are Responsive.</h4></p>"

}

 

 

#Outputs header and formatting for Last 24 hours health watcher alerts

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h5>Last 24 Hours Health Watcher Alerts</h5>"

 

 

#Counts agent heartbeat failure within the last 24 hours

$targetdate = (get-date).AddDays(-1)

$Count = get-SCOMalert | where-object {($_.TimeRaised -gt $targetdate) -and ($_.Name -eq "Health Service Heartbeat Failure")} | group-object Name | Measure-Object

if($Count.Count -gt 0) {

                        $ReportOutput += get-SCOMalert | where-object {($_.TimeRaised -gt $targetdate) -and ($_.Name -eq "Health Service Heartbeat Failure")} | group-object Name | Select count,name | sort-object count -descending | ConvertTo-HTML

} else {

$ReportOutput += "<p><h4>No Health Service Heartbeat Failures Recieved.</h4></p>"  

}

 

 

#Outputs header and formatting for agents in pending state

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h5>Agents in Pending State</h5>"

 

 

#Checks if any agents are currently pending approval

$Count = Get-SCOMPendingManagement | sort AgentPendingActionType | Measure-Object

if($Count.Count -gt 0) {

                        $ReportOutput += Get-SCOMPendingManagement | sort AgentPendingActionType | select AgentName,ManagementServerName,AgentPendingActionType | ConvertTo-HTML

} else {

$ReportOutput += "<p><h4>No Agents are Pending Approval.</h4></p>" 

}

 

 

#Outputs header and formatting for Agent Load Balancing

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h5>Agent Load Balancing</h5>"

 

 

#Checks agent load balancing over System Center – Operations Manager 2012

$ReportOutput += get-SCOMagent | sort Name | Group PrimaryManagementServerName -Noelement | sort Name | Select Count,Name | ConvertTo-HTML

 

#Outputs header and formatting for agent patch list

 

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

$ReportOutput += "<h5>Agent Patch List</h5>"

 

 

#Queries SQL operations database and counts current agent patch levels 

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

$SqlConnection.ConnectionString = "Server=$OpsDatabaseSQLServer;Database=$OpsDatabaseName;Integrated Security=True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd.CommandText = "select COUNT (hs.patchlist) AS 'Count' , hs.patchlist AS 'Patch_Level' from MT_HealthService hs

GROUP BY Patchlist

 

"

 

$SqlCmd.Connection = $SqlConnection

 $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter

 $SqlAdapter.SelectCommand = $SqlCmd

 $DataSet = New-Object System.Data.DataSet

 $SqlAdapter.Fill($DataSet)

 $SqlConnection.Close()

 $OpsAgentPatchSQLQuery = $dataSet.Tables[0].rows | Select-Object Count , Patch_Level

 $ReportOutput += $OpsAgentPatchSQLQuery | ConvertTo-HTML

 

 

#Outputs line for end of report format purposes

$ReportOutput += "</br>"

$ReportOutput += "<hr size=4 width=50% align=left>"

 

 

Now run using the command from the previous blog to start the script, replacing parameters as required, as illustrated in this example:

 

PowerShell.exe Daily-Report.ps1 MSServerName c:\scripts Daily-Report.htm

 

This will produce a report similar to the below :

 

 

 

 

Daily-Report.ps1