Forum Discussion

brama's avatar
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?

8 Replies

  • Also 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's avatar
      Mike_Moniz
      Icon for Professor rankProfessor

      Actually that will not help with the alert email, nm!

      • brama's avatar
        brama

        That already works indeed, but I need it in the mail alert itself :-)

  • Outputs 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.

    • brama's avatar
      brama

      Yes, but I want the email to include the filename that triggers the alert.

      • Justin_Lanoue's avatar
        Justin_Lanoue
        Icon for Advisor rankAdvisor

        If 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##.