SCOM PowerShell Script for Maintenance Mode by IP address match
August 12, 2009 Leave a comment
The web seems to bring us a seemingly endless supply of PowerShell scripts for manipulating maintenance mode in SCOM 2007. I had occasion to write yet another maintenance mode script recently and thought it may be worth throwing out there. This one will put objects (Windows Servers) in maintenance mode when the agent IP address matches a string. This script can be useful to schedule maintenance mode when a remote site will be taken down for maintenance or patching.
#Connect to the RMS server and initialize the command shell
$rmsServerName=”<SERVERNAME>”
add-pssnapin “Microsoft.EnterpriseManagement.OperationsManager.Client”;
Set-Location “OperationsManagerMonitoring::”;
$mgConn = New-ManagementGroupConnection -connectionString:$rmsServerName;
if($mgConn -eq $null)
{
[String]::Format(“Failed to connect to RMS on ‘{0}'”,$rmsServerName);
return;
}
Set-Location $rmsServerName;#Set up maintenance mode variables
$time = [DateTime]::Now
$nMinutes=1440$class=get-monitoringclass | where {$_.displayname -eq “Windows Server”}
Function StartMM($agent){
$objMon=get-monitoringobject -ID $agent.id
write-host “Starting Maintenance Mode for: ” $objMon.displayname
New-MaintenanceWindow -MonitoringObject $objMon -Comment “Suppressing IP network with a script” -StartTime $time -EndTime $time.AddMinutes($nMinutes)
}get-agent |where {$_.IPAddress -like “192.168.1.*”} | ForEach-Object {StartMM $_}
get-agent |where {$_.IPAddress -like “192.168.2.*”} | ForEach-Object {StartMM $_}
To end maintenance mode, just a few slight modifications to the main body of the script:
#Set up maintenance mode variables
$time = [DateTime]::Now.AddMinutes(3)
write-host $time
$time = [DateTime]::Now.AddMinutes(5)$class=get-monitoringclass | where {$_.displayname -eq “Windows Server”}
Function EndMM($agent){
$objMon=get-monitoringobject -ID $agent.id
write-host “Ending Maintenance Mode for: ” $objMon.displayname
Set-maintenancewindow -EndTime $time -monitoringobject $objmon
}get-agent |where {$_.IPAddress -like “192.168.1.*”} | ForEach-Object {EndMM $_}
get-agent |where {$_.IPAddress -like “192.168.2.*”} | ForEach-Object {EndMM $_}
A creative use for this script behind the cut: