MOM 2005 Maintenance Mode through PowerShell

 
 
If you are still using MOM 2005 like the client I work for, you probably use some tool like
the MaintenanceModeUtility from the MOM 2005 SDK when you need to put multiple servers
in Maintenance mode.
We use(d) to use it, but I never liked this tool, it’s pretty slow because it needs to connect to MOM for every
single server and doesn’t have decent logging for success or failures.
 
Since MOM 2005 is pre-Powershell it doesn’t have built-in CmdLets like SCOM 2007,
 but there are .NET Assemblies that come with the product, so why not leverage those!
 
Using the C# source code that comes with the existing tool, I decided to port (part of it) it to PowerShell.
I also took a look at following excellent post that helped me get started:
 
You need to create a csv-file (srvlist.txt) which uses following header-format: Server,Domain
and fill it up with the servers you want to put in Maintenance mode.
Customize the length of your maintenance period and the reason and run the script.
It needs to be run from a Management Server, so you need Powershell there.
 
 
 
#Init
#Make sure Maintenance period in minutes is correct
$MaintPeriod=10
#Customize reason for maintenance mode if necessary
$Reason="Maintenance"
# End Init
$LogFilePath="c:\log.txt"
Remove-Item $LogFilePath
[System.Reflection.Assembly]::LoadFile("$($env:ProgramFiles)\Microsoft Operations Manager 2005\SDK Bin\Microsoft.Mom.Sdk.dll")
[System.Reflection.Assembly]::LoadFile("$($env:ProgramFiles)\Microsoft Operations Manager 2005\mom.context.dll")
$mom = [Microsoft.EnterpriseManagement.Mom.Administration]::GetAdministrationObject()
$cm = $mom.GetComputerMaintenanceObject()
$computerQuery = $mom.GetComputerQueryObject()
 
function ActivateMaintMode ($server,$domain,$MaintPeriod,$Reason)   {
   $foundComputer = $computerQuery.GetComputerByNameAndDomain($server, $domain)
   if ($foundcomputer -and !$foundcomputer.IsInMaintenanceMode) {
      $endMaintenanceAt=(get-date).ToUniversalTime().addminutes($MaintPeriod)
      $cm.SetMaintenanceMode($foundcomputer, $endMaintenanceAt, $Reason)
      Out-File -append -filepath $LogFilePath -inputobject "$server from domain $domain has been placed in Maintenance mode for $Maintperiod minutes"
   }
   else {
      if ($foundcomputer.IsInMaintenanceMode) {Out-File -append -filepath $LogFilePath -inputobject "$server from domain $domain is already in Maintenance mode until $($foundComputer.MaintenanceOutTime.ToLocalTime()) – maintenance mode not updated" }
      else {Out-File -append -filepath $LogFilePath -inputobject "$server from domain $domain not found in MOM" }
      }
}
 
Foreach($Machine in (import-csv ".\srvlist.txt")) {
ActivateMaintMode $Machine.Server $Machine.Domain $MaintPeriod $Reason
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s