0

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)

Steve P answered