none
PS Monitoring in SCOM not working as expected RRS feed

  • General discussion

  • Hello Guys,

    i have been working on Powershell Monitor in SCOM whch monitor the status of a service on three server.

    This service should be runnong on one server at time. 

    So I have created below script which works perfectly fine in Powershell but giving false alerts when configured it in SCOM

    _____________________________________________________________________

    $omemailapi = New-Object -comObject 'MOM.ScriptAPI'
    $omemailbag = $omemailapi.CreatePropertyBag()

    $APOM=Get-Service -Name "OMEmailService" -ComputerName "X"| select -ExpandProperty Status
    $EMOM=Get-Service -Name "OMEmailService" -ComputerName "Y"| select -ExpandProperty Status
    $AMOM=Get-Service -Name "OMEmailService" -ComputerName "Z"| select -ExpandProperty Status

    if(($APOM -eq "Stopped" -and $EMOM -eq "Running" -and $AMOM -eq "Stopped") -or ($APOM -eq "Stopped" -and $EMOM -eq "Stopped" -and $AMOM -eq "Running") -or ($APOM -eq "Running" -and $EMOM -eq "Stopped" -and $AMOM -eq "Stopped"))
    {
    $omemailbag.AddValue("OMStatus","OMOK")
    }
    Else
    {
    $omemailbag.AddValue("OMStatus","OMError")
    $omemailbag.AddValue("APServer","$APOM")
    $omemailbag.AddValue("EUServer","$EMOM")
    $omemailbag.AddValue("AMServer","$AMOM"
    }
    $omemailbag
    __________________________________________________________________________________________
     

    thanks in advance for your help guys

    Friday, March 23, 2018 8:03 PM

All replies

  • How do you knw it is giving false alerts?

    It is best to format your code correctly so that it is readable and so that you can see the errors and typos.  Also avoid unnecessary quoting as it can have side effects.

    $omemailapi = New-Object -comObject MOM.ScriptAPI
    $omemailbag = $omemailapi.CreatePropertyBag()
    
    $APOM=Get-Service -Name OMEmailService -ComputerName X| select -ExpandProperty Status
    $EMOM=Get-Service -Name OMEmailService -ComputerName Y| select -ExpandProperty Status
    $AMOM=Get-Service -Name OMEmailService -ComputerName Z| select -ExpandProperty Status
    
    if(($APOM -eq 'Stopped' -and $EMOM -eq 'Running' -and $AMOM -eq 'Stopped') -or ($APOM -eq 'Stopped' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Running') -or ($APOM -eq 'Running' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Stopped')){
        $omemailbag.AddValue('OMStatus','OMOK')
    }else{
        $omemailbag.AddValue('OMStatus','OMError')
        $omemailbag.AddValue('APServer',$APOM)
        $omemailbag.AddValue('EUServer',$EMOM)
        $omemailbag.AddValue('AMServer',$AMOM)
    }
    $omemailbag
    

    Your code had syntax errors and closure issues.


    \_(ツ)_/

    Friday, March 23, 2018 8:25 PM
  • Seems like there is repetitive code. I would probably refactor to something like:


    $omEmailApi = New-Object -ComObject MOM.ScriptAPI
    $omEmailBag = $omEmailApi.CreatePropertyBag()
    
    $serverList = "X","Y","Z"
    
    $errorOccurred = $false
    foreach ( $server in $serverList ) {
      $service = Get-Service OMEmailService -ComputerName $server
      if ( $service.Status -ne [ServiceProcess.ServiceControllerStatus]::Running ) {
        $errorOccurred = $true
        $omEmailBag.AddValue(...)  # put whatever is appropriate here
      }
    }
    if ( $errorOccurred ) {
      # Do whatever is appropriate here
    }
    


    -- Bill Stewart [Bill_Stewart]


    Friday, March 23, 2018 8:34 PM
    Moderator
  • Hi Jrv,

    Sorry, I just pasted it from monitor script tab.However, I had this script in format in PS ISE.

    Below is the service status of OMEmailService

    Server A = Stopped

    Server B=Running

    Server C=Stopped

    As per defined IF condition, Monitor Status should be Healthy because OMEmailService Service is only running on Server B which is true as per IF Condition.

    I have verified that monitor configuration are fine. however, i am still seeing this alert that OMEmailService Status is unhealthy


    Friday, March 23, 2018 10:32 PM
  • Please review your logic expression.  It is doing what you wrote it to do.  If that is not what you want then you will need to re-design if.

    if(
            ($APOM -eq 'Stopped' -and $EMOM -eq 'Running' -and $AMOM -eq 'Stopped') `
            -or 
                ($APOM -eq 'Stopped' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Running') `
            -or 
                ($APOM -eq 'Running' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Stopped')
        ){
        $omemailbag.AddValue('OMStatus','OMOK')
    }else{

    The logic says that, if any one service is running then the it is "OK"

    Restared in a more readable form:

    if(
            ($EMOM -eq 'Running' -and $APOM -eq 'Stopped' -and $AMOM -eq 'Stopped') `
            -or 
            ($AMOM -eq 'Running' -and $APOM -eq 'Stopped' -and $EMOM -eq 'Stopped') `
            -or 
            ($APOM -eq 'Running' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Stopped')
        ){
        $omemailbag.AddValue('OMStatus','OMOK')
    }

    This will only return false if all services are not running or if more than one service is running.


    \_(ツ)_/


    • Edited by jrv Friday, March 23, 2018 10:45 PM
    Friday, March 23, 2018 10:44 PM
  • I think this is what you are trying to do:

    $omemailapi = New-Object -comObject MOM.ScriptAPI
    $omemailbag = $omemailapi.CreatePropertyBag()
    
    $APOM=(Get-Service -Name OMEmailService -ComputerName X).Running -eq 'Running'
    $EMOM=(Get-Service -Name OMEmailService -ComputerName Y).Running -eq 'Running'
    $AMOM=(Get-Service -Name OMEmailService -ComputerName Z).Running -eq 'Running'
    
    $omemailbag.AddValue('APServer',$APOM)
    $omemailbag.AddValue('EUServer',$EMOM)
    $omemailbag.AddValue('AMServer',$AMOM)
    
    if($APOM -or $EMOM -or $AMOM){
        $omemailbag.AddValue('OMStatus','OMOK')
    }else{
        $omemailbag.AddValue('OMStatus','OMError')
    }
    $omemailbag


    \_(ツ)_/

    Friday, March 23, 2018 10:54 PM
  • Thank you everyone for your help and guiding me toward a solution.

    finally i am able to achieve the required output with below script

    $omemailapi = New-Object -comObject 'MOM.ScriptAPI'
    $omemailbag = $omemailapi.CreatePropertyBag()
    
    
    $APOMPOM=Get-Service -Name 'Omemailservice' -ComputerName "Orch-ap-om02"| select -ExpandProperty Status
    $EMOM=Get-Service -Name 'Omemailservice' -ComputerName "Orch-eu-om02"| select -ExpandProperty Status
    $APOMMOM=Get-Service -Name 'Omemailservice' -ComputerName "Orch-us-om02"| select -ExpandProperty Status
    
    if(
            ($EMOM -eq 'Stopped' -and $APOM -eq 'Stopped' -and $AMOM -eq 'Stopped') `
            -or 
            ($AMOM -eq 'Stopped' -and $APOM -eq 'Running' -and $EMOM -eq 'Running') `
            -or 
            ($APOM -eq 'Running' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Running') `
            -or
            ($APOM -eq 'Running' -and $EMOM -eq 'Running' -and $AMOM -eq 'Stopped') `
            -or
            ($APOM -eq 'Running' -and $EMOM -eq 'Running' -and $AMOM -eq 'Running')
        ){
        $omemailbag.AddValue('OMStatus','OMError')
        $omemailbag.AddValue('APServer',$APOM)
        $omemailbag.AddValue('EUServer',$EMOM)
        $omemailbag.AddValue('AMServer',$AMOM)
    }
    Else
    
    {
    
        $omemailbag.AddValue('OMStatus','OMOK')
    }
    
    $omemailbag
    
    $omemailapi.Return($omemailbag)
    


    Saturday, March 31, 2018 6:44 PM
  • Don't look now but:

    This:

    if(
            ($EMOM -eq 'Stopped' -and $APOM -eq 'Stopped' -and $AMOM -eq 'Stopped') `
            -or 
            ($AMOM -eq 'Stopped' -and $APOM -eq 'Running' -and $EMOM -eq 'Running') `
            -or 
            ($APOM -eq 'Running' -and $EMOM -eq 'Stopped' -and $AMOM -eq 'Running') `
            -or
            ($APOM -eq 'Running' -and $EMOM -eq 'Running' -and $AMOM -eq 'Stopped') `
            -or
            ($APOM -eq 'Running' -and $EMOM -eq 'Running' -and $AMOM -eq 'Running')
        ){

    is identical logically to this:

    if($EMOM -eq 'Stopped' -or $APOM -eq 'Stopped' -or $AMOM -eq 'Stopped'){ `
    


    \_(ツ)_/

    Saturday, March 31, 2018 6:53 PM