Schedule a group of URLs into Maintenance Mode

I needed to schedule a group of URLs into Maintenance Mode.  I already created a script to schedule one URL but setting up 30 would be a pain to manage.  So I created a new script to schedule a group of URLs.


URLGroupMM.zip

Usage: 
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\URLGroupMM.ps1 -rootMS scomrms1 -group ‘AU 3AM Website Reboot Group’ -minutes 120 -comment ’3AM AU Websites Reboot’ -reason ‘PlannedOther’


param($rootMS,$groupName,$minutes,$comment,$reason)

 

Add-PSSnapin “Microsoft.EnterpriseManagement.OperationsManager.Client” -ErrorVariable errSnapin;

Set-Location “OperationsManagerMonitoring::” -ErrorVariable errSnapin;

new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin;

set-location $rootMS -ErrorVariable errSnapin;

 

$groupObject = get-monitoringobject | where {$_.DisplayName -eq $groupName};

$groupagents = $groupObject.getrelatedmonitoringobjects()

 

foreach ($agent in $groupAgents)

 

{

 

$URLStuff = $agent.displayname

 

$URLStuff

 

 

$URLWatcher = (Get-MonitoringClass -name Microsoft.SystemCenter.WebApplication.Perspective) | Get-MonitoringObject | where {$_.DisplayName -eq $URLStuff}

$startTime = [System.DateTime]::Now

$endTime = $startTime.AddMinutes($minutes)

 

 

“Putting URL into maintenance mode”

New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$URLWatcher -comment:$comment -Reason:$reason

 

}

7 Comments

  1. MatthiasR:

    Hi Tim,
    thank you for sharing this.
    There is a mistake at the code:
    $groupObject = get-monitoringobject | where {$_.DisplayName -eq “AU 3AM Website Reboot Group”};
    The group name should be replaced with the variable “$groupName”

    Regards

    Matthias

  2. Tim:

    MatthiasR

    Good eyes. I put that in there to test it and forgot to change it back. I have fixed the zip and the post.

    Thanks,

    Tim

  3. Paul:

    Thanks, this was extremely helpful!

  4. Sven:

    I tried this URL group maintenance mode powershell script and it doesn’t appear to work.
    I am getting the following error:

    You cannot call a method on a null-valued expression.
    At D:\Temp\URLGroupMM.ps1:9 char:56
    + $groupagents = $groupObject.getrelatedmonitoringobjects( <<<< )
    Putting URL into maintenance mode
    New-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' be
    cause it is null.
    At D:\Temp\URLGroupMM.ps1:26 char:81
    + New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObje
    ct:$ <<<Powershell D:\Temp\URLGroupMM.ps1 -rootMS rtprms01.americas.ppdi.local -group ‘DMART Web Application Monitor Group’ -minutes 30 -reason ‘Daily maintenance mode’

    Thanks,
    Sven

  5. Jacob:

    What object type(s) did you add to the custom group?

  6. Tim:

    Objects of type Web Application Perspective.

  7. Tim:

    Make sure your group only contains objects of type “Web Applicaiton Perspective”

Leave a comment