Putting a Group of Computers into Maintenance Mode via Powershell


image Have you ever found yourself in need of putting a group of systems into Maintenance Mode in System Center Operations Manager 2007 R2?  If so you can use the script below.  Note that in SCOM 2007 R2 there is no need to put the health service and health service watcher into Maintenance Mode separately, we just need to put the computer itself into Maintenance Mode and it will take care of other components automatically.

*************Script*********

param($rootMS,$group,$numberOfHoursInMaintenanceMode,$comment)

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 $group};
$groupagents = $groupObject.getrelatedmonitoringobjects()

foreach ($agent in $groupAgents)

{

$computerPrincipalName = $agent.displayname
$computerPrincipalName

$computerClass = get-monitoringclass -name:Microsoft.Windows.Computer
$healthServiceClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthService
$healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher

$computerCriteria = "PrincipalName=’" + $computerPrincipalName + "’"
$computer = get-monitoringobject -monitoringclass:$computerClass | Where{$_.Displayname -like $computerPrincipalName}
$startTime = [System.DateTime]::Now
$endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode)

"Putting " + $computerPrincipalName + " into maintenance mode"
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer -comment:$comment

}

*************************

Save the above script with a ps1 extension.

Schedule the script using the Task Scheduler and command line below:

Powershell c:\MMode.ps1 -rootMS: <RMS server name> -group: <Group name> – Numberofhoursinmaintenancemode: 1 -Comment: ‘This is a test’

Hope this helps,

Ritesh Grover | System Center Technical Lead

Comments (7)

  1. Anonymous says:

    Has anyone tried this on OM 2012? I need a PS script to put servers into Maintenance mode on 2012. Thanks!

  2. Anonymous says:

    I would really like to do something similar, where the script would point to a text file with a list of servers.  It would place all servers in that file, into Maintenance Mode.

    Is this possible?  We are currently doing this with MOM2005.

  3. Ernie says:

    Hello,

    Is there a way to input the name of prompt the user to enter the name of the group each time the script is run. For example what I would like to do is add this script as a Task under Windows Computer view. Then when executed a a popup appears asking for the name of the group (or better still selection from a drop down list (pre populated list) group name is typed in or selected and OK pressed etc to execute.

    Thanks very much

    Ernie

  4. pickmeindia says:

    Your thought processing is wonderful. The way you tell the thing is awesome. You are really a master. Great Blog!! That was amazing

    <a href="http://www.pickmeindia.com/&quot; title= “Mobile Repair” ><b>Mobile Repair

  5. richuk_69 says:

    Here is a function I created to put a server into maintenance mode run the function and the function call is

    Add-ObjectInMaintenanceMode -DisplayName Name_Of_Object

    Enjoy!!

    Function Add-ObjectInMaintenanceMode
    {
    Param
    (
    [cmdletbinding(PositionalBinding=$false)]
    [Parameter(Mandatory=$true)][string]$DisplayName
    )
    #add the SnapIn for SCOM
    Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client

    #create a new powershell provider
    New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root:

    $ManagementGroupConnection = "mgmtisopsmgr.office.zen.co.uk"

    #create a new Management Group connection
    New-ManagementGroupConnection $ManagementGroupConnection

    #change location to the All Computers group
    sl Monitoring:$ManagementGroupConnectionMicrosoft.SystemCenter.AllComputersGroup

    <#If the object is being monitored get the Display Name#>
    $MonitoringObject = (Get-Monitoringclass -Name "Microsoft.Windows.Computer" | Get-Monitoringobject | Where {$_.DisplayName -eq "$DisplayName"})

    <#Is the object in maintenance mode?#>
    $IsItInMaint = (Get-Maintenancewindow -erroraction ‘silentlycontinue’ -MonitoringObject $MonitoringObject).ScheduledEndTime

    <#The length of time in minutes that the object is in maintenance mode#>
    [int]$AddMinutes = 10

    <#If the object is not in Maintenance Mode but is monitored put into maintenance mode#>
    If([bool]$IsItInMaint -eq $false -and [bool]$MonitoringObject -eq $true)
    {
    Write-Host "The server ""$DisplayName"" will be put into Maintenance Mode…" -ForeGroundColor Green

    New-MaintenanceWindow `
    -StartTime (Get-Date) `
    -EndTime $((Get-Date).AddMinutes($AddMinutes)) `
    -Reason PlannedOther `
    -Comment "AUtomated Testing" `
    -MonitoringObject (Get-Monitoringclass -Name "Microsoft.Windows.Computer" | Get-Monitoringobject | Where {$_.DisplayName -eq "$DisplayName"})

    }

    <#If the object is in Maintenance Mode and is monitored extend the maintenance window#>
    ElseIf([bool]$IsItInMaint -eq $true -and [bool]$MonitoringObject -eq $true)
    {
    Write-Host "The server ""$DisplayName"" is already in Maintenance Mode so will be extended by 60 mins…" -ForeGroundColor Green
    New-MaintenanceWindow `
    -StartTime (Get-Date) `
    -EndTime $(($IsItInMaint).AddMinutes($AddMinutes)) `
    -Reason PlannedOther `
    -Comment "AUtomated Testing" `
    -MonitoringObject (Get-Monitoringclass -Name "Microsoft.Windows.Computer" | Get-Monitoringobject | Where {$_.DisplayName -eq "$DisplayName"})

    }

    <#If the object is not in Maintenance Mode and is not monitored do nothing#>
    ElseIf([bool]$IsItInMaint -eq $false -and [bool]$MonitoringObject -eq $false)
    {
    Write-Host "The server ""$DisplayName"" is not being monitored in SCOM" -ForeGroundColor Yellow
    }
    }

  6. richuk_69 says:

    Following on here is a function to remove an object out of maintenance mode the function call is

    Remove-ObjectInMaintenanceMode -DisplayName Name_Of_Object

    Function Remove-ObjectInMaintenanceMode
    {
    Param
    (
    [cmdletbinding(PositionalBinding=$false)]
    [Parameter(Mandatory=$true)][string]$DisplayName
    )
    #add the SnapIn for SCOM
    Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client

    #create a new powershell provider
    New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root:

    $ManagementGroupConnection = "mgmtisopsmgr.office.zen.co.uk"

    #create a new Management Group connection
    New-ManagementGroupConnection $ManagementGroupConnection

    #change location to the All Computers group
    sl Monitoring:$ManagementGroupConnectionMicrosoft.SystemCenter.AllComputersGroup

    <#If the object is being monitored get the Display Name#>
    $MonitoringObject = (Get-Monitoringclass -Name "Microsoft.Windows.Computer" | Get-Monitoringobject | Where {$_.DisplayName -eq "$DisplayName"})

    <#Is the object in maintenance mode?#>
    $IsItInMaint = (Get-Maintenancewindow -erroraction ‘silentlycontinue’ -MonitoringObject $MonitoringObject).ScheduledEndTime

    <#The length of time in minutes that the object is in maintenance mode#>
    [int]$AddMinutes = 10

    <#If the object is in maintenance mode and is being monitored take the object out of maintenance mode#>
    If([bool]$IsItInMaint -eq $true -and [bool]$MonitoringObject -eq $true)
    {
    Write-Host "Maintenance Mode for the server ""$DisplayName"" will be removed" -ForeGroundColor Green
    Set-Maintenancewindow `
    -EndTime (Get-Date) `
    -Reason PlannedOther `
    -Comment "Task Completed" `
    -MonitoringObject (Get-Monitoringclass -Name "Microsoft.Windows.Computer" | Get-Monitoringobject | Where {$_.DisplayName -eq "$DisplayName"}) `
    }

    <#If the object is not in maintenance mode and is being monitored there is nothing to perform#>
    ElseIf([bool]$IsItInMaint -eq $false -and [bool]$MonitoringObject -eq $true)
    {
    Write-Host " The server ""$DisplayName"" is already out of Maintenance mode so not tasks to perform" -ForeGroundColor Green
    }

    <##If the object is not in maintenance mode and is not being monitored the object is not in SCOM#>
    ElseIf([bool]$IsItInMaint -eq $false -and [bool]$MonitoringObject -eq $false)
    {
    Write-Host " The server ""$DisplayName"" is not being monitored by SCOM" -ForeGroundColor Yellow
    }
    }

  7. show box says:

    Thanks for the great info. I really loved this. I would like to apprentice at the same time as you amend your web site, how could i subscribe for a blog site?
    For more info on showbox please refer below sites:
    http://showboxandroids.com/showbox-apk/
    http://showboxappandroid.com/
    Latest version of Showbox App download for all android smart phones and tablets.
    http://movieboxappdownloads.com/ – It’s just 2 MB file you can easily get it on your android device without much trouble. Showbox app was well designed application for android to watch movies and TV shows, Cartoons and many more such things on your smartphone.
    For showbox on iOS (iPhone/iPad), please read below articles:
    http://showboxappk.com/showbox-for-ipad-download/
    http://showboxappk.com/showbox-for-iphone/
    Showbox for PC articles:
    http://showboxandroids.com/showbox-for-pc/
    http://showboxappandroid.com/showbox-for-pc-download/
    http://showboxforpcs.com/
    There are countless for PC clients as it is essentially easy to understand, simple to introduce, gives continuous administration, effectively reasonable. it is accessible at completely free of expense i.e., there will be no establishment charges and after establishment
    it doesn’t charge cash for watching films and recordings.
    http://www.showboxforipad.org/showbox-apk/ Not simply watching, it likewise offers alternative to download recordings and motion pictures. The accompanying are the strides that are to be taken after to introduce Showbox application on Android. The above
    all else thing to be done is, go to the Security Settings on your Android telephone, Scroll down and tap on ‘Obscure sources’.
    http://www.showboxforipad.org/
    http://movieboxappdownloads.com/moviebox-apk-android/
    http://movieboxappdownloads.com/download-moviebox-pc/
    Movie Box, an esteemed movies application in which you can find stacks of programs and films. The guide is given here to download Movie Box app to Android and to Apple iOS 9.0.2, iOS 8.4/8.3 and also for the lower versions without Jailbreak.
    http://showboxforiphone.org/
    Please do login to Showbox application with the help of Ymail. You can login in Ymail from here –
    http://ymaillogintips.com/
    Sign Up & Do registration for latest movies on Showbox application