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
3 property bags objects but only 2 alerts
Peter Svensson
211
Reputation points
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?
Accepted answer
-
CyrAz 5,181 Reputation points
2020-09-01T08:03:05.237+00:00