SCOM Group members Maintenance Mode
I do realize that this script is for a some what dated product, but it is still being covered by main stream support so I see no harm in making it available for the SCOM peers.
The feature to note is we have refrained from using a "| where" filter and have persevered to make the execution as tight as possible. So the script is meant to put all agents in the targeted group into maintenance mode. We later added some checks to avoid putting management servers in to maintenance mode, since they will (or should) be handled by the SCOM Administrators only. This, however,was done by depending on naming conventions used in the company. So I will post another Script with the ability to exclude an array of Monitoring Classes from the maintenance setting.
Param
(
[parameter(Position = 0)]
[String]
$RMS='nstmc973nnm1.eng.wintel.Fabrikameng.net',
[parameter(Position = 1)]
[String]
$GroupName='Fabrikam Semaphore BCAAA Server Group'
)
If ((Get-PSSnapin | where-Object { $_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client' }) -eq $null)
{
Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue -ErrorVariable Err
}
If ((Get-PSDrive | where-Object { $_.Name -eq 'Monitoring' }) -eq $null)
{
New-PSDrive -Name:Monitoring -PSProvider:OperationsManagerMonitoring -Root:\ -ErrorAction SilentlyContinue -ErrorVariable Err | Out-Null
}
#Connect to RMS
Set-Location "OperationsManagerMonitoring::"
new-managementGroupConnection -ConnectionString:$RMS | Out-Null
Set-Location Monitoring:\RMS
#$GroupName='Fabrikam Server Group'
$grps = Get-MonitoringObjectGroup
ForEach ($grp in $grps)
{
If ($grp.DisplayName -eq $GroupName)
{$grp_maintenancetarget = $grp}
}
$agents = $grp_maintenancetarget.GetRelatedMonitoringObjects()
$compclass = Get-MonitoringClass -name 'Microsoft.Windows.Computer'
$Main_Duration= 3 #In Hours
$comment = 'Set to Maintenance mode by script on \ttsserver\C\Fabrikam\Scom_files\MaintenanceMode, requested by XYZ team'
#Can be set as a parameter if needed
$targetarr = @()
ForEach ($agent in $agents)
{
$criteria = "PrincipalName='" + $agent.DisplayName + "'"
$basecomp = $compclass | Get-MonitoringObject -Criteria $criteria
$targetarr += $basecomp
}
ForEach ($comp in $targetarr)
{
$now = Get-Date
$end = $now.AddHours(3)
$now
Write-Host ("Start of task: " + (Get-Date) + "`r`n" + "Putting " + $comp.DisplayName + " into maintenance mode")
New-MaintenanceWindow -startTime:$now -endTime:$end -monitoringObject:$comp -comment:$comment
Write-Host ("End of task: " + (Get-Date))
}