3 property bags objects but only 2 alerts

Peter Svensson 211 Reputation points
2020-09-01T05:11:16.023+00:00

I have a script that checks alerts from an api and returns a propertybag for each obejct it finds. The script is run by an Alert Rule (PS) snippet from Kevin Holmans library

I can see in my logs (Operations Manager) that it outputs 3 "System.__ComObject". However, I only get 2 alerts. It always ignores the first one.
Suppression is turned off in the rule.

The foreach loop below:

    foreach($object in $objects)
    {

            $ID = $object.id
            $Severity = $object.severity
            $Time = get-epochDate $object.time_noticed
            $ResolveTime = get-epochDate $object.resolve_time
            $ErrorSpec = $object.specifier
            $Message = $object.causes | out-string
            $Resolved = $object.resolved


            $LastError = $Time
            #$CurrentDate = (Get-Date).AddMinutes(-15)
            $CurrentDate = (Get-Date).AddHours(-24)



              if ($LastError -gt $CurrentDate){

                $propertyBag.AddValue('Result',"BAD")
             $propertyBag.AddValue('Error',"$Message")
             $scriptOutput += "Error found: $object`n"
                $scriptOutput += "ID: $ID`n"   

              ### Add property bag values ###
              $propertybag.AddValue('WebApi',"$WebAPI")
              $propertybag.AddValue('ID',"$ID")
              $propertybag.AddValue('Resolved',"$Resolved")
              $propertybag.AddValue('Severity',"$Severity")
              $propertybag.AddValue('Time',"$Time")
              $propertybag.AddValue('Message',"$Message")


              ### Create a new property bag
              $propertyBag = $momScriptAPI.CreatePropertyBag();

              ### Return property bag
              $scriptOutput += "Propertybag $propertyBag`n"   
              $propertyBag


              }#if


    }#foreach

Any suggestions?

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,413 questions
0 comments No comments
{count} votes

Accepted answer
  1. CyrAz 5,181 Reputation points
    2020-09-01T08:03:05.237+00:00

    You create the propertybag at the end of the loop, so the first time the loop runs all $propertybag.Addvalue() commands are failing and the first propertybag returned is empty.
    You have the create the propertybag before you add anything to it

    0 comments No comments

0 additional answers

Sort by: Most helpful