Adding additional text in mail from script
Hi,
Small question, I have created a custom datasource that uses a embedded powershell script:
# Variables
$Folder = '##ps.filecount.folder##';
$Minutes = '##ps.filecount.minutes##';
$Recurse = '##ps.filecount.recursive##';
$Exclude = '##ps.filecount.exclude##';
$ComputerName = '##system.displayname##';
# Build parameters dynamically
$GetChildItemParams = @{
Path = $Folder
Attributes = '!Directory+!System'
}
if ($Recurse) {
$GetChildItemParams['Recurse'] = $true
}
# Create session dynamically
$Session = New-PSSession -ComputerName $ComputerName
try {
# Execute remotely
$Result = Invoke-Command -Session $Session -ScriptBlock {
param (
$Folder,
$Minutes,
$Recurse,
$Exclude,
$GetChildItemParams
)
if (-not (Test-Path $Folder)) {
# Exit with special error code if folder missing
exit 3
}
[array]$Files = Get-ChildItem @GetChildItemParams |
Where-Object { $_.LastWriteTime -le (Get-Date).AddMinutes(-$Minutes) -and $_.FullName -notmatch "$Exclude" -and $_.FullName -notmatch "nagios-monitoring" -and $_.FullName -notmatch "netscaler-monitoring" }
# Get the newest file among all (not just those older than $Minutes)
$NewestFile = $Files | Sort-Object LastWriteTime -Descending | Select-Object -First 1
# Safely extract properties
$NewestFilePath = if ($NewestFile) { $NewestFile.FullName } else { "" }
$NewestFileDate = if ($NewestFile) { $NewestFile.LastWriteTime } else { "" }
# Return both count and newest file path
return @{
Count = $Files.Count
NewestFile = $NewestFilePath
NewestDate = $NewestFileDate
}
} -ArgumentList $Folder, $Minutes, $Recurse, $Exclude, $GetChildItemParams
# Output only the final datapoint in the correct format
Write-Output "NumberofFiles=$($Result.Count)"
Write-Output "NewestFile=$($Result.NewestFile -replace '\\', '\\')"
Write-Output "NewestFileDate=$($Result.NewestDate)"
Write-Output "INFO: Found $($Result.Count) file(s) older than $Minutes minutes in $Folder on $ComputerName"
}
catch {
# Optional: Output something recognizable for LogicMonitor if there's an error
Write-Output "NumberofFiles=-1"
Write-Output "NewestFile=ERROR"
Write-Output "NewestFileDate=ERROR"
}
finally {
# Cleanup the session (always!)
if ($Session) {
Remove-PSSession -Session $Session
}
}The Graph is fine (NumberofFiles), but I also want to include the "NewestFile" and "NewestFileDate" output in the mail alert.
is this possible?
Bram Assendorp
Posted 1 year ago·Last reply 1 year ago
8 comments
Mike Moniz
·1 year agoAlso if you only need to determine the filename during an active alert, you will see your raw output (including text) in the Poll Now window and you can include the filename there.
Mike Moniz
·1 year agoActually that will not help with the alert email, nm!
Bram Assendorp
OP1 year agoThat already works indeed, but I need it in the mail alert itself :-)
Justin Lanoue
·1 year agoOutputs need to be numeric.
Therefore you need to translate the Error into like 1 and then on the threshold make it = 1 to alert and define a note saying like:
Success=0, Error=1,2=Unknown
As for dates you would probably want to convert it into like NewestFileDate_DaysAgo and alert when its > x days ago.
Bram Assendorp
OP1 year agoYes, but I want the email to include the filename that triggers the alert.
Justin Lanoue
·1 year agoIf you're okay with only checking every 15 minutes, you could turn this into an active discovery script and make the filename the wildalias or a property that gets discovered and then in your alert email reference the instance or property name with like ##INSTANCE## or ##PROPERTYNAME##.
Bram Assendorp
OP1 year agoDid not check it, could not find any good documentation about this how to implement.
Justin Lanoue
·1 year agoI don't currently have time to write it but basically you want the active discovery script out to be like below in the format of "WILDVALUE##WILDALIAS##DESCRIPTION"
Write-Host "NewestFile##NewestFile##NewestFileDate"
This will discover an instance.
Then to output values on that instance in your collection script, you need to format like "NewestFile.NumberofFiles=Value"
It's a little messy and you might want to have the discovery script discover like a "Dummy" instance to display your NumberofFiles datapoint always and then when that file you want to alert on gets discovered, maybe have script logic to only alert on instances that aren't called "Dummy" with an "Alert" datapoint if that makes sense.