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

 

}

9 Responses to Schedule a group of URLs into Maintenance Mode

  1. MatthiasR February 11, 2009 at 10:37 am #

    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 February 11, 2009 at 10:49 am #

    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 October 17, 2009 at 6:19 pm #

    Thanks, this was extremely helpful!

  4. Sven February 22, 2010 at 10:58 am #

    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

    • Tim April 27, 2010 at 7:07 pm #

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

  5. Jacob March 24, 2010 at 8:28 am #

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

    • Tim April 27, 2010 at 7:02 pm #

      Objects of type Web Application Perspective.

  6. Lewis July 3, 2012 at 7:57 am #

    Please Assist not sure what is wrong

    I am doing the following steps….

    Using Web Application Transaction Monitoring
    Add monitoring Wizard
    Web Application Transaction Monitoring
    Give A name and Select a custom mgmt pack
    Type the URL http://www.hotmail.com and Click Test
    Select a Watch node.

    I add another URL using the above method
    Url: http://www.google.com

    So now i have 2 URL for monitoring

    Next i create a custom group called “URL monitoring”
    using “Objects of type Web Application Perspective”, i add both the names.

    Then i run the powershell command

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe c:\URLGroupMM.ps1 -rootMS: ‘FhGscomprod’ -GroupName: ‘URL Monitoring’ -minutes:5 -comment: ‘My Comment’ -reason: ‘PlannedOther’

    I still get an error….

    You cannot call a method on a null-valued expression.
    At C:\URLGroupMM.ps1:10 char:56
    + $groupagents = $groupObject.getrelatedmonitoringobjects <<<< ()
    + CategoryInfo : InvalidOperation: (getrelatedmonitoringobjects:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Putting URL into maintenance mode
    New-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' because it is null.
    At C:\URLGroupMM.ps1:27 char:81
    + New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject: <<<< $URLWatcher -comment:$comment –
    Reason:$reason
    + CategoryInfo : InvalidData: (:) [New-MaintenanceWindow], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.EnterpriseManagement.Operations
    Manager.ClientShell.NewMaintenanceWindowCmdlet

  7. Orhan September 25, 2012 at 12:59 pm #

    If you have multiple watcher node for the same URL then script fill fail because multiple objects are being enumerated for that just add another foreach loop. Here is the working one;

    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}

    foreach ($eachurl in $URLWatcher)
    {

    $startTime = [System.DateTime]::Now
    $endTime = $startTime.AddMinutes($minutes)

    “Putting URL into maintenance mode”

    New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$eachurl -comment:$comment -Reason:$reason
    }
    }

Leave a Reply