Scom Powershell Monitor Issue

Hi there.
I’m new at the powershell integration (monitoring) and need som help.
I created a simple powershell script to monitor files.

When I implement the script using a monitor, I do not recieve any alerting.
Nor Do I receive any error in the Operations manager eventvwr on the target

When running the script localy and checking the popertybag, I got the correct results (that should raise an alert):
<DataItem type=“System.PropertyBagData” time=“2020-05-12T14:20:20.0485214+02:00” sourceHealthServiceId=“068B2D64-D7B6-49AE-6A4F-441091E8BA21”><Property Name=“State” VariantType=“8”>
Critical</Property><Property Name=“ReturnList” VariantType=“8”>
FullName CreationTime State


F:\share\Files1\subfolder\test.ft1 5/12/2020 12:49:03 PM Critical
F:\share\Files1\test.ft1 5/12/2020 11:12:30 AM Critical
F:\share\Files2\test_2.ft2 5/12/2020 11:21:10 AM Critical
F:\share\Files3\test.ft3 5/12/2020 12:46:35 PM Critical

</Property></DataItem>

in my monitor, I have the following settings:
Parameters: “F:\share\Files1*.ft1”,60,120,recurse;“F:\share\Files2*.ft2”,80,3600;“F:\share\Files3*.ft3”,100,3600
Evaluation:
Unhealhty Expression: Property[@Name=‘State’] Equals Critical
Warning Expression: Property[@Name=‘State’] Equals Warning
Healthy Expression: Property[@Name=‘State’] Equals Healthy

Script:
####################
# this scripts monitors the age of files
# It accepts 1 input parameter
# This single parameter contains a 2 dimensional aray
# First dimension separator = “;”
# Second dimention separator = “,”
#
# In the first dimension you can specify the parameters of the first searcgh you want to conduct
# In the first dimention, you need to specify
# a file location (string, mandatory)
# a value in seconds when you want to have a warning alert (integer, mandatory)
# a value in seconds when you want to have a critical alert (integer, mandatory)
# the key word “recurse” if you want to search sub folders (string, optional)
# example : “C:\Myfilelocation\Patern*.log,60.120”
#
# using the second dimention, you can specify muliple values for the first
# example: “C:\Myfilelocation1*.log,60,120;C:\Myfilelocation2*.log,60,120;C:\Myfilelocation3*.log,60,120”
#
####################
param([string]$FileConditions)
# for debugging
# $FileConditions = “F:\share\Files1*.ft1,60,120,recurse;F:\share\Files2*.ft2,80,3600;F:\share\Files3*.ft3,100,3600”

$ScomAPI = New-Object -comObject “MOM.ScriptAPI”
$PropertyBag = $ScomAPI.CreatePropertyBag()

#as optimists, we start by setting the default Global status on healthy
$GlobalState = “Healthy”

#prepare aray for output
$ReturnAray = @()

#split the input in peaces
$Folderaray = $FileConditions.Split(";")
foreach ($type in $Folderaray)
{
$typearay=$type.Split(",")
#validate your input
if ((Test-Path $typearay[0]) -and ($typearay[1] -match “^\d+$”) -and ($typearay[2] -match “^\d+$”))
{
#recursive
if (($typearay.count -eq 4) -and ($typearay[3] -eq “recurse”))
{
$checkFiles = Get-ChildItem -path $typearay[0] -Recurse
}
#Not recursive
elseif ($typearay.count -eq 3)
{
$checkFiles = Get-ChildItem -path $typearay[0]
}

foreach ($checkFile in $checkFiles)
{
#check for critical
if ($checkFile.CreationTime -lt (Get-Date).AddSeconds(-$typearay[2]))
{
$ReturnAray +=,$checkFile | select Fullname, CreationTime, @{Name=“State”;Expression={“Critical”}}
$GlobalState = $GlobalState.replace(“Healthy”,“Critical”)
$GlobalState = $GlobalState.replace(“Warning”,“Critical”)
}
#check for warning
elseif ($checkFile.CreationTime -lt (Get-Date).AddSeconds(-$typearay[1]))
{
$ReturnAray +=,$checkFile | select Fullname, CreationTime, @{Name=“State”;Expression={“Warning”}}
$GlobalState = $GlobalState.replace(“Healthy”,“Warning”)
}
}
}
}
$ReturnString = $ReturnAray | out-string

$PropertyBag.AddValue(“State”,$GlobalState)
#$PropertyBag.AddValue(“ReturnList”,$ReturnString)

# Send output to SCOM
$PropertyBag

#for Debugging
#$ScomAPI.Return($PropertyBag)

Are you using this MP?: https://cookdown.com/scom-essentials/powershell-authoring/

The native SCOM Monitor Script wizard does not support PowerShell.

Yes, Im using PowerShell Monitoring - Community Management Pack.

The script runs fine interactively/localy on the server

I get the expected results from $ScomAPI.Return($PropertyBag) , but the alert that responds to the “state” is not triggered.

Im currently investigating if this is an UAC issue …

Something to consider…

When the script runs, it will run under the context of the agent. If your agent runs as “system,” make sure that “system” has access to the folders/files.

I would also recommend using a UNC path and not a mapped folder. In my experience, I could never get mapped drives to work reliably, permitting the server object (servername$) to the folder, and using the UNC path always worked for me.

Your line for adding to the property bag uses the double quotes around State: $PropertyBag.AddValue(“State”,$GlobalState)

In the parameter name field for the monitor’s healthy and unhealthy expressions, are you using double quotes? SCOM’s UI defaults to single quotes. If it doesn’t match, the alert does not seem to trigger properly. Try setting it to: Property[@Name=“State”]

I have had this issue a few times and that has fixed it for me.