Quantcast
Channel: Operations Manager - General forum
Viewing all articles
Browse latest Browse all 11941

SCOM 2012 powershell slow to put server into maintenance mode

$
0
0

i have a powershell script that works in conjunction with ESM remedy.When ESM Remedy puts servers into unmanaged mode.A powershell remote command will be sent to the SCOM server.

Problem is it takes up to a min and 20 secs to put 1 server into maintenance mode.If we put multiple servers into maintenance mode,there will also be resource issues from teh ESM server.

Upgraded Powersehll to version 3 and created a profile but there is still no improvement (reduced duration) in the time it takes to put a single server into MM.

Script below that i run on the SCOM server to test

param($inputString)
#$inputString = "XXXXXXXXXXX,UNMANAGE,2,This is a test comment,SYSTEM,Request data stamp,Expiry date stamp"
#$inputString = "XXXXXXXXXXX,MANAGE,2,This is a test comment,SYSTEM,Request data stamp,Expiry date stamp"

# Set Management Server
#strPrimaryMS = "XXXXXXXXXXX"
$strPrimaryMS = "localhost"

# Grab script name
$myName = $MyInvocation.MyCommand.Name

# Change Default Exception Stop Behaviour
$strOldSetting = $ErrorActionPreference
$ErrorActionPreference = 'Stop' # Previously set to: SilentlyContinue

# Add SCOM Script API for Writing Error Logs
Try
{
   $objMomApi = New-Object -comObject 'MOM.ScriptAPI'
}
Catch
{
   # Exit Script
   Exit -1
}

#LogScriptEvent Severities
$momErr = 1
$momWarn = 2
$momInfo = 4

#Event ids to use
$errID = 11000
$warnID = 11001
$infoID = 11002

# Log Start of Script
$objMomApi.LogScriptEvent($myName, $infoID , $momInfo , "Beginning unmaintenance PowerShell Script.")

# Check Parameters
If ((-not $inputString))
{
   $objMomApi.LogScriptEvent($myName, $errID , $momErr , "Problem with parameters. Exiting unmaintenance PowerShell Script.")

   # Exit Script
   Exit -1
}

# Add Module: OperationsManager
Try
{
   If (-not (Get-Module OperationsManager))
   {
      Import-Module OperationsManager
   }
}
Catch
{
   $objMomApi.LogScriptEvent($myName, $errID , $momErr , "$_.")
                  
   # Exit Script
   Exit -1
}

# Connect to Operations Manager Management Group
Try
{
   Start-OperationsManagerClientShell -managementServerName $strPrimaryMS -persistConnection:$false -interactive:$false
}
Catch
{
   $objMomApi.LogScriptEvent($myName, $errID , $momErr , "$_.")
  
   # Remove Module: OperationsManager
   Get-Module OperationsManager | Remove-Module
  
   # Exit Script
   Exit -1
}

#Consts
$manageOn = "MANAGE"
$manageOff = "UNMANAGE"

$arr_Input =  $inputString.Split(",")

$str_Host = $arr_Input[0]  #This is the incoming host to unmanage
$str_Mode = $arr_Input[1]  #This is the mode being requested
$str_interval = $arr_Input[2] #This is the duration in hours to unmanage
$str_Reason = $arr_Input[3]  #A text comment on the reason for unmanaging
$str_User = $arr_Input[4]  #Who has requested the unmanage on the web page

#Supplied target will be in shortname format - so resolve to the FQDN
$str_target = [net.dns]::GetHostByName($str_Host).Hostname

#Clear up outstanding Alerts.
Get-SCOMAlert | Where {$_.ResolutionState -eq 0 -and $_.Name -eq "Unmanage Notification (SNMP)"} | Set-SCOMAlert -ResolutionState 255 | Out-Null

If($str_target -eq $Null){
   #Couldnt resolve to FQDN so quit out
   $objMomApi.LogScriptEvent($myName, $errID , $momWarn , "Unable to resolve to FQDN skipping: " + $str_Host)
   Return
}

# Debug Log: Found FQDN of server
$objMomApi.LogScriptEvent($myName, $infoID , $momInfo , "Found FQDN:  " +$str_Target)

$computer = Get-SCClass -Name "Microsoft.Windows.Computer" | Get-SCOMClassInstance | Where-Object {$_.DisplayName -eq $str_Target}

$endTime = ((Get-Date).AddHours($str_interval))

If($computer -eq $Null){
   $objMomApi.LogScriptEvent($myName, $infoID , $momInfo , "Unknown Computer requested for MaintenanceMode, skipping: " + $str_Target)
   Return
}

If(($computer.InMaintenanceMode -eq $false) -and ($str_Mode -eq $manageOff))
{
   Try
   {
   $MMComment = $str_Reason + "`r`nBy:" + $str_User
      Start-SCOMMaintenanceMode -Instance $computer -EndTime $endTime -Comment $MMComment
   }
   Catch
   {
      $objMomApi.LogScriptEvent($myName, $errID , $momWarn , "ERROR putting " + $computer.DisplayName + " into maintenance mode:: " + $_.Exception.Message)
   }
}

If(($computer.InMaintenanceMode -eq $true) -and ($str_Mode -eq $manageOn))
{
   Try
   {
      $MMComment = $str_Reason + "`r`nBy:" + $str_User
      Get-SCOMMaintenanceMode -Instance $computer | Set-SCOMMaintenanceMode -EndTime (Get-Date) -Comment $MMComment
  
      $objMomApi.LogScriptEvent($myName, $infoID , $momInfo , "Success Ending " + $computer.DisplayName + " maintenance mode")
   }
   Catch
   {
      $objMomApi.LogScriptEvent($myName, $errID , $momWarn , "ERROR ending " + $computer.DisplayName + "  maintenance mode:: " + $_.Exception.Message)
   }
}

# Set Default Exception Stop Behaviour Back to Previous Setting
$ErrorActionPreference = $strOldSetting

# Log successful execution of script
$strExecutionResult = "Completed unmaintenance PowerShell Script."
$objMomApi.LogScriptEvent($myName, $infoID , $momInfo , $strExecutionResult)

Exit 0
# End of Script

Any advice would be great on how to reduce the time to put a server into MM.

cheers


Viewing all articles
Browse latest Browse all 11941

Trending Articles