Maintenance Mode via Powershell Remoting

There are loads of scripts and GUIs that you can use to set Maintenance Mode in OpsMgr, but if you want to do this from a server that doesn’t have the OpsMgr-snapins for Powershell it’s a bit harder…

But then there is Powershell v2 and Remoting… It gives you the option to run a scriptblock on another computer…

Just enable remoting on your RMS and then try this script from another machine:

Function setMaintMode {
        PARAM (
                [string] $rmsHostname,
                [string] $agentName,
                [string] $Comment,
                [string] $Reason,
                [int] $Time
        )

        Invoke-Command -ComputerName $rmsHostname -scriptblock {
                PARAM (
                        [string] $agentName,
                        [string] $Comment,
                        [string] $Reason,
                        [int] $Time
                )

                Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"
                Set-Location "OperationsManagerMonitoring::"
                New-ManagementGroupConnection -ConnectionString:localhost | Out-Null

                $computerClass = Get-MonitoringClass -name:Microsoft.Windows.Computer
                $computerCriteria = "PrincipalName='" + $agentName + "'"
                $computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteria

                if ($computer.InMaintenanceMode -eq $false) {
                        $startTime = [System.DateTime]::Now
                        $endTime = $startTime.AddMinutes($Time)

                        New-MaintenanceWindow -startTime $startTime -endTime $endTime -Comment $comment -Reason $Reason -monitoringObject $computer
                        return $true
                }  else {
                        # Allready in maintenance mode
                        return $false
                }

        } -ArgumentList $agentName, $Comment, $Reason, $Time
}

setMaintMode -rmsHostname "rmsserver.domain.local" -agentName "currentserver.domain.local" -Comment "Some comment" -Time 30 -Reason "PlannedOperatingSystemReconfiguration"

What it does is that it run’s the OpsMgr-specific parts on the RMS instead on your local machine… so with that in place it’s easy to create a GUI around it and then spread a shortcut to all your servers that have Powershell v2 installed.

Notes:
The quick and dirty way to enable remoting on your rms, start cmd as an administrator and run winrm quickconfig
Here can you find a quick intro to PS Remoting.


MaintMode on a single class

Didnt know that this would work…

In a project that I’m working on the customer wanted me to create a Management Pack for a couple of services, the only problem was that they wanted to restart the services during the night.
My first solution was to create a group with the computer in it and run a PowerShell script to set the computer in to maintenance mode.

Then I tested to put my own class in that group and use the same script on the class instead.

Worked like a charm :-)

The script: systemcenterforum.org/maintenance-mode-scheduled-task-for-ops-mgr-2007/

Maintenance Mode