question

PeterSvensson-4019 avatar image
0 Votes"
PeterSvensson-4019 asked CyrAz answered

3 property bags objects but only 2 alerts

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?

msc-operations-manager
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

CyrAz avatar image
0 Votes"
CyrAz answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.