Share via


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.

  1. Param

  2.     (

  3.     [parameter(Position = 0)]

  4.     [String]

  5.     $RMS='nstmc973nnm1.eng.wintel.Fabrikameng.net',

  6.     [parameter(Position = 1)]

  7.     [String]

  8.     $GroupName='Fabrikam Semaphore BCAAA Server Group'   

  9.     )

  10. If ((Get-PSSnapin | where-Object { $_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client' }) -eq $null) 

  11. {  

  12. Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue -ErrorVariable Err 

  13. If ((Get-PSDrive | where-Object { $_.Name -eq 'Monitoring' }) -eq $null) 

  14. New-PSDrive -Name:Monitoring -PSProvider:OperationsManagerMonitoring -Root:\ -ErrorAction SilentlyContinue -ErrorVariable Err | Out-Null 

  15. #Connect to RMS

  16.  

  17. Set-Location "OperationsManagerMonitoring::" 

  18. new-managementGroupConnection -ConnectionString:$RMS | Out-Null 

  19. Set-Location Monitoring:\RMS

  20.  

  21. #$GroupName='Fabrikam Server Group'

  22. $grps = Get-MonitoringObjectGroup

  23. ForEach ($grp in $grps)

  24. {

  25.     If ($grp.DisplayName -eq $GroupName)

  26.     {$grp_maintenancetarget = $grp}

  27. }

  28. $agents = $grp_maintenancetarget.GetRelatedMonitoringObjects()

  29. $compclass = Get-MonitoringClass -name 'Microsoft.Windows.Computer'

  30. $Main_Duration= 3 #In Hours

  31. $comment = 'Set to Maintenance mode by script on \ttsserver\C\Fabrikam\Scom_files\MaintenanceMode, requested by XYZ team'

  32. #Can be set as a parameter if needed

  33. $targetarr = @()

  34. ForEach ($agent in $agents)

  35. {

  36.     $criteria = "PrincipalName='" + $agent.DisplayName + "'"

  37.     $basecomp = $compclass | Get-MonitoringObject -Criteria $criteria

  38.     $targetarr += $basecomp

  39. }

  40. ForEach ($comp in $targetarr)

  41. {

  42.     $now = Get-Date

  43.     $end = $now.AddHours(3)

  44.     $now

  45.     Write-Host ("Start of task: " + (Get-Date) + "`r`n" + "Putting " + $comp.DisplayName + " into maintenance mode")

  46.     New-MaintenanceWindow -startTime:$now -endTime:$end -monitoringObject:$comp -comment:$comment

  47.     Write-Host ("End of task: " + (Get-Date))

  48. }