Introduction
This is the 17th instalment of the Automating OpsMgr series. Previously on this series:
- Automating OpsMgr Part 1: Introducing OpsMgrExtended PowerShell / SMA Module
- Automating OpsMgr Part 2: SMA Runbook for Creating ConfigMgr Log Collection Rules
- Automating OpsMgr Part 3: New Management Pack Runbook via SMA and Azure Automation
- Automating OpsMgr Part 4:Creating New Empty Groups
- Automating OpsMgr Part 5: Adding Computers to Computer Groups
- Automating OpsMgr Part 6: Adding Monitoring Objects to Instance Groups
- Automating OpsMgr Part 7: Updated OpsMgrExtended Module
- Automating OpsMgr Part 8: Adding Management Pack References
- Automating OpsMgr Part 9: Updating Group Discoveries
- Automating OpsMgr Part 10: Deleting Groups
- Automating OpsMgr Part 11: Configuring Group Health Rollup
- Automating OpsMgr Part 12: Creating Performance Collection Rules
- Automating OpsMgr Part 13: Creating 2-State Performance Monitors
- Automating OpsMgr Part 14: Creating Event Collection Rules
- Automating OpsMgr Part 15: Creating 2-State Event Monitors
- Automating OpsMgr Part 16: Creating Windows Service Monitors
Now that I have demonstrated how to create basic Windows service monitors using New-OMServiceMonitor, in this post, I’ll demonstrate how to use the OpsMgrExtended module to create an instance of the “Windows Service” management pack template:
The OpsMgrExtedned module comes with a function called New-OMWindowsServiceTemplateInstance. Same as all other functions in this module, it is fully documented, you can access the help document using Get-Help cmdlet:
Get-Help New-OMWindowsServiceTemplateInstance –Full
Sample Runbook New-WindowsServiceTemplateInstance
Workflow New-WindowsServiceTemplateInstance
{
Param(
[Parameter(Mandatory=$true)][String]$InstanceDisplayName,
[Parameter(Mandatory=$false)][String]$InstanceDescription,
[Parameter(Mandatory=$true)][String]$TargetGroupName,
[Parameter(Mandatory=$true)][String]$ServiceName,
[Parameter(Mandatory=$false)][String]$LocaleId,
[Parameter(Mandatory=$true)][Boolean]$CheckStartupType,
[Parameter(Mandatory=$false)][Int]$CPUPercent,
[Parameter(Mandatory=$false)][Int]$MemoryUsageMB,
[Parameter(Mandatory=$false)][Int]$ConsecutiveSampleCount,
[Parameter(Mandatory=$false)][Int]$PollIntervalInSeconds
)
#Get OpsMgrSDK connection object
$OpsMgrSDKConn = Get-AutomationConnection -Name "OpsMgrSDK_Home"
#Hard code which MP to use
$MPName = "TYANG.Test.Windows.Monitoring"
#Make sure MP exists
Write-Verbose "Getting management pack '$MPName'"
$MP = Get-OMManagementPack -SDKConnection $OpsMgrSDKConn -Name $MPName -ErrorAction SilentlyContinue
If ($MP -eq $null)
{
#MP doesn't exist, create it
Write-Verbose "management pack '$MPName' does not exist. creating now."
$CreateMP = New-OMManagementPack -SDKConnection $OpsMgrSDKConn -Name $MPName -DisplayName "TYANG Test Windows Monitoring" -Version "1.0.0.0"
}
#parameters
#Create Service Monitor, MP Version will be increased by 0.0.0.1
$result = InlineScript
{
$SDK = $USING:OpsMgrSDKConn.ComputerName
Write-Verbose "OpsMgr management server name: $SDK"
$UserName = $USING:OpsMgrSDKConn.Username
$password = ConvertTo-SecureString -AsPlainText $USING:OpsMgrSDKConn.Password -force
$parms = @{
SDKConnection = $USING:OpsMgrSDKConn
MPName = $USING:MPName
DisplayName = $USING:InstanceDisplayName
Description = $USING:InstanceDescription
ServiceName = $USING:ServiceName
TargetGroupName = $USING:TargetGroupName
CheckStartupType = $USING:CheckStartupType
IncreaseMPVersion = $true
}
if ($USING:LocaleId -ne $null)
{
$parms.Add('LocaleId', $USING:LocaleId)
}
if ($USING:CPUPercent -gt 0)
{
$parms.Add('PercentCPU', $USING:CPUPercent)
}
if ($USING:MemoryUsageMB -gt 0)
{
$parms.Add('MemoryUsage', $USING:MemoryUsageMB)
}
if ($USING:ConsecutiveSampleCount -gt 0)
{
$parms.Add('ConsecutiveSampleCount', $USING:ConsecutiveSampleCount)
}
if ($USING:PollIntervalInSeconds -gt 0)
{
$parms.Add('PollIntervalInSeconds', $USING:PollIntervalInSeconds)
}
Write-Verbose "Calling New-OMWindowsServiceTemplateInstance with the following parameters:"
Write-Verbose ($parms | out-string)
New-OMWindowsServiceTemplateInstance @parms
}
If ($result)
{
Write-Output "The Windows Service monitoring template instance `"$InstanceDisplayName`" is created."
} else {
Write-Error "Unable to create the Windows Service monitoring template instance `"$InstanceDisplayName`"."
}
}
I have hardcoded the following parameters in the runbook:
- SMA OpsMgr connection object name (which you will need to change to suit your environment)
- (Unsealed) MP (where the rule is going to be saved to) – “TYANG.Test.Windows.Monitoring”
Additionally, this runbook will firstly try to retrieve the management pack from the management group, if the MP deosn’t exist, it will create it first.
This runbook takes the following input parameters:
- InstanceDisplayName – The Display name of the template instance.
- InstanceDescription – This is an optional parameter. The description of the template instance.
- TargetGroupName – The name of the target group
- ServiceName – The name of the Windows service (i.e. w32time)
- LocaleId - The 3-letter MP language pack locale ID. This is an optional parameter, if not specified, it will be set to “ENU”.
- CheckStartupType – Set this Boolean parameter to True if you only want to monitor automatic service. More details about this parameter can be found from Kevin Holman’s blog post Monitoring Windows Services – Automatic, Manual, and Disabled, using CheckStartupType
- CPUPercent – Specify the threshold for CPU Usage Percentage. This is an optional parameter, if not specifiied, the CPU Usage will not be monitored or collected.
- MemoryUsageMB – Specify the threshold for Memory Usage (MB). This is an optional parameter, if not specifiied, the Memory Usage will not be monitored or collected.
- ConsecutiveSampleCount – Specify the the number of (consecutive) samples for the CPU and Memory counters, This is an optional parameter. if not specified, the value is set to 2 (which is the default value when using the wizard in the OpsMgr console).
- PollingIntervalInSeconds - Specify sample polling interval (in seconds). This is an optional parameter, if not specifiied, the value is set to 300 (which is the default value when using the wizard in the OpsMgr console).
To help you visualise what are these parameters mean, I’ve mapped them to the fields in the GUI wizard:
Runbook Execution Result:
A Known Issue
When I was writing the sample runbook, I found a small bug in the OpsMgrExtended module. You may noticed from the screenshots above, that the description field is not populated. I have found the cause of this issue and fixed it in my lab. This fix will be shipped with the next release. So please just be aware of this issue, I don’t think it’s too critical.
Conclusion
In this post, I have demonstrated how to create Windows Service MP template instances using the OpsMgrExtended module. This concludes the topics of monitoring Windows services. In the next module, I will demonstrate how to create any types of generic rules by specifying the configuration of each member module (Data Source, Condition Detection and Write Action) using the New-OMRule function. Until next time, happy automating!
Leave a comment