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

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.

This first entry will serve as an introduction, explaining how PowerShell can be used to extract information from Operations Manager to show the health state of management servers and then output this onto an HTML page. Management servers have been exemplified in the first instance as their optimal health is critical to Operations Manager being able to provide connection for SDK clients, configuration generation/distribution and workload distribution through resource pools. Subsequent posts will build on this principle to allow more sophisticated system health reporting.


Step 1  

The first four lines in the script Daily-report.ps1 attached below allow data to be passed from the command line that is used to start the script and write values to variables for later use: 

#ParamString1 - MSServerName = The FQDN of the Management Server 

#ParamString2 - FileLocation = The Folder you want the report to be saved to

#ParamString3 - ReportName = What you want the report to be called 



The above line of code would take the following command when run and map it to the following parameters:

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


                               ParamSring1 ParamString2 ParamString3



#Checks if Report of same name already exists and deletes if found 

$FullPath = "$Filelocation\$ReportName"

if ((test-Path -path $FullPath) -ne $False)

{remove-item -Path $FullPath}


Step 2 

Now to connect to System Center Operations Manager 2012 using the management server name from parameter $MSServerName written from Paramstring1:

# Imports OpsMgr 2012 PowerShell Module and connects to the Management server specified in ParamString1

Import-Module OperationsManager 

New-SCOMManagementGroupConnection -ComputerName:$MSServerName 


Step 3

This section of the script allows you modify the appearance of the HTML report: 

#Opens $ReportOutput for input, the <style> tag then is used to define style information for the HTML document 

$ReportOutput= "<style>


#The <body> element contains all the contents of an HTML document

body {font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}


# The table tag below defines the HTML table

table {border-collapse: collapse border: none; font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;color: black;margin-bottom: 10px;}

# The <th> tag defines the table header

th {font-size: 12px; font-weight: bold; padding-left: 0px; padding-right: 20px; text-align: left;}


#The <td tag element defines the table cell

td{font-size: 12px; padding-right: 20px; text-align: left;}


#The <h1> to <h5> tags are used to define HTML headings for text

h1{ clear: both; font-size: 24pt; margin-left: 20px; margin-top: 30px;}

h2{ clear: both;font-size: 115%; margin-left: 20px; margin-top: 30px;}

h3{clear: both; color:red; font-size: 10pt;margin-left: 20px; margin-top: 30px;}

h4{clear: both; color:green; font-size: 10pt;margin-left: 20px; margin-top: 30px;}

h5{ clear: both; font-size: 16pt; margin-left: 20px; margin-top: 30px;}


#This defines the even rows of an HTML table 

table tr:nth-child(even) { background: #DBE5F1; } 


#This defines the odd rows of an HTML table

table tr:nth-child(odd) { background: #EAF1DD; }


#</style> tag is used to close the defined style information for the HTML document



# outputs HTML for Title of document on report

$ReportOutput +="<h1><center>System Center 2012 - Operations Manager Report</center></h1>"


# Gets current date and time and displays on output report 

$targetdate = (get-date) 

$ReportOutput += "<h2>Report run on: $targetdate</h2>" 


Step 4 

Next, the queries to run against Operations Manager are entered: in this case focusing primarily on the health of the management server as previously mentioned.

#Creates a line break in the HTML.   

$ReportOutput += "<br>"


#Title for the query using the header specified in the Style section

$ReportOutput += "<h5>Management Server State</h5>"


#Creates a separating line for formatting purposes in the HTML 

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


#Checks if Management Servers are in a Healthy State

$ReportOutput += "<h5>Management Servers not in a Healthy State</h5>"

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

 if($Count.Count -gt 0) {

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

 } else {

       $ReportOutput += "<h4>All management servers are in healthy state.</h4>"      



#Checks if Management Servers are in Maintenance Mode

$ReportOutput += "<br>"

 $ReportOutput += "<h5>Management Servers in Maintenance Mode</h5>"

 $MSs = get-scomgroup -displayname "Operations Manager Management Servers" |get-scomclassinstance


 foreach ($MS in $MSs)


 if($MS.inmaintenancemode -eq "False")

 { $ReportOutput += "<h3>$MS.DisplayName is in Maintenance Mode </h3>"

 } else {

       $ReportOutput += "<h4>$MS.DisplayName is not in Maintenance Mode </h4>"



#Checks for Management Server Open Alerts

$ReportOutput += "<br>"

$ReportOutput += "<h5>Management Server Open Alerts</h5>"

$ManagementServers = Get-SCOMManagementServer

foreach ($ManagementServer in $ManagementServers){

     $ReportOutput += "<p><h5>Alerts on " + $ManagementServer.ComputerName + "</h5></p>"

     $ReportOutput += get-SCOMalert -Criteria ("NetbiosComputerName = '" + $ManagementServer.ComputerName + "'") | where {$_.ResolutionState -ne '255' -and $_.MonitoringObjectFullName -Match 'Microsoft.SystemCenter'} | select TimeRaised,Name,Description,Severity | ConvertTo-HTML



#Creates a separating line for formatting purposes in the HTML

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


Step 5 

Now the final step is to display the report. This example will just output to screen but it can be saved to SharePoint and displayed through the Operations Manager console. Another popular option is to script an automatic email of the generated report.

  #Converts all output from $ReportOutput to $Filelocation

ConvertTo-HTML -head $Head -body "$ReportOutput" | Out-File $FullPath


#The </body> element closes the contents of an HTML

$ReportOutput += "</body>"


#Invokes HTML output file and opens
invoke-item $FullPath


#Clears $ReportOutput variable for future use
Clear-Variable ReportOutput


Now save the file and run using the command from step 1 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: