8 hours ago
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?