Home > PowerShell > Powershell: How to get the SCOM maintenance mode history of a computer

Powershell: How to get the SCOM maintenance mode history of a computer

Today I got a requirement to see if a computer account was placed in maintenance mode or yesterday. If yes, I would like to see maintenance window details and the username who did it.

Given that there no GUI option in SCOM to get this information, I explored the powershell way and finally came-up with below script.

function Get-SCOMMaintenanceModeHistroy {            

[cmdletbinding()]
param (
 [parameter(mandatory=$true)]
 [string]$computerName,
 [parameter(mandatory=$true)]
 [string]$RootmanagementServer            

)            

Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction 0 | Out-Null
new-managementGroupConnection -ConnectionString:$RootmanagementServer | out-null
Push-Location -Path "OperationsManagerMonitoring::" -ErrorAction:Stop
$agent = Get-Agent | Where-Object {$_.displayname -match $ComputerName}
$MaintenanceObj = Get-MaintenanceWindow -MonitoringObject $agent.HostComputer -History -ErrorAction 0
  if($MaintenanceObj) {
   $output = New-Object -Type PSObject -Property @{
    ComputerName = $Computer
    StartTime  = ($MaintenanceObj.Starttime).tolocaltime()
    EndTime   = ($MaintenanceObj.ScheduledEndTime).Tolocaltime()
    UserName  = $MaintenanceObj.User
    Comment   = $MaintenanceObj.Comments
   }
   $Output | select ComputerName, StartTime, EndTime, UserName, Comment | ft -wrap
  } else {
   write-Error "Unable to get the maintenance mode of $ComputerName"
  }
Pop-Location
}

There are some SQL queries around the web and you can use them if you want. You can find one such thing at http://sys-man.blogspot.com/2011/03/scom-2007-r2-maintenance-mode-history.html. I am not sure about the functionality of this SQL code. Please get this tested in test bed before you try in production.

Hope this helps and happy learning…

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>