This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Advanced File Age Monitor with PowerShell

Hi,


I need to monitor the age of file in a folder. I can not use the default File Age monitor because it does not accept a wildcard for filename.

The files that are in the folder have different names, hence the need to use another File Age monitoring approach.


I've found a PowerShell script online that does the trick but I am having difficulties getting it to work in SAM. It could be that I don't fully understand what SAM expects as an output.

Hopefully someone here can help so that we all can benefit from it. I know a lot of people have requested the ability to monitor File Age when file does not have a set name.


Here's the PowerShell script:


$fullPath = "C:\hp\hpsmh\certs"
$numdays = 3
$numhours = 10
$nummins = 5
function ShowOldFiles($path, $days, $hours, $mins)
{
$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and
($_.psIsContainer -eq $false)})
if ($files -ne $NULL)
{
for ($idx = 0; $idx -lt $files.Length; $idx++)
{
$file = $files[$idx]
write-host ("Old: " + $file.Name) -Fore Red
}
}
}
ShowOldFiles $fullPath $numdays $numhours $nummins

Attached screenshots show a test script and the expected output (file name in red). The other screenshot shows the error displayed in SAM when performing a test.

Are there any SAM/PowerShell gurus around that ould modify the script so that it works with SAM?

attachments.zip
  • SAM is expecting 2 lines to come back, one a message (optional), and another a statistic.  Your output doesn't match the format that SAM is after.  I'd do the following:

    $path = 'c:\temp'

    $stats = 0

    $msg = ''

    $days = 3

    $hours = 10

    $mins = 5

    $files = @(Get-ChildItem -Recurse -Path $path -Include '*.*' | ?{ $_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins) -and $_.psIsContainer -eq $false})

    if ($files -ne $null) {

      $f_names = [System.String]::Join('|',$files)

      $msg = 'Message: ' + $f_names

      $stats = $files.Count

    } else {

      $msg = 'Message: 0 files exceed defined age'

    }

    Write-Host $msg

    Write-Host "Statistic: $stats"

    WIth this, it'll output the file names in the Message, and the file count that exceeds the age will be a statistic, and you can monitor/alert based on that value. One thing to be careful of is the length that the message will get, depending on the number of files in the path.

    Edit: Corrected a 'typo' on line 22 which originally said 'Statistics' rather than the correct verbiage 'Statistic', and corrected variables $minutes to $mins

  • Jonathan,

    I have a current need to monitor for a wildcard file extension and this seems to work great, however, what do you think is causing this error in the Template Monitor itself?

    If I execute the script from the PowerShell CMD line then it works great, but this same SCRIPT inside a SAM PowerShell Monitor fails with this message:

    PowerShell script error. Script output values are not defined or improperly defined.






  • lchance

    The script already looks for wildcard extensions so there shouldn't be any issues there.  The other issue is that I cannot type apparently.  Line 22 should be 'Statistic' not 'Statistics'.  I wrote it correctly in the answer to the question, but my sample script was broken.  This is what happens when you write quick scripts at home away from your lab.  I'll update the sample script above to ensure it's correct and others don't trip over my typing abilities emoticons_happy.png

  • Jonathan where can I get the Template for the Powershell File Age Monitor, I don't see it available on our Solarwinds server under the Thwack templates or the standard ones.  Thanks Karlita

  • krcourser if you're referring to my code above, I didn't put it into a template, but they are not hard to create yourself.

    • Go to SAM Settings
    • Click "Create New Template"
    • Give the template a name
    • Click on "Add Component Monitors"
    • Select "Manually Add Component Monitors"
    • Go through the list and look for Windows PowerShell

    From here it's a case of filing in the details.  There will be a a box to click for the "script" where you can paste in the above.  Adjust the code to fit your needs.  Test it against a node you want to validate, and you should get results.

  • Awesome, thanks so much. I’m fairly new to scripting and Orion so I have been doing a lot of testing, trials, error, frustration…. We used to use a product called Zabbix and it seemed hard but we managed to get what we needed to work with it but trying to get all those monitors to transfer to Orion has proven to be a challenge when trying to monitor log files and database tables.

    I really appreciate your help and guidance.

    Karlita

  • Jonathan,

    I got the results I am looking for just need to clarify the defined age part, if I don’t want them to be more than 20 minutes old do I just have to change $days = 0, $hours = 0, and $mins = 20. I am assuming the $_.LastWriteTime –lt… means last write time less than the given totals in those variables. Like I said very new to most scripting and especially Powershell, I know just enough to be dangerous. I get time to learn a bit but once I get done what is needed do get to use it again for months to years so have to relearn it all again. I am a very big fan of documentation for this reason.

    Thanks again for your help.

    Karlita

  • Jonathan,

    Sorry to be a pain but it all works when testing while creating the Template but once I try and assign it, to the same node I tested against, I get the following output, any ideas.

  • Hey krcourser,

    Are you running that script in local or remote mode? In either case, can you double-check that the path exists?

  • Used both and yes the path exists, used unc path and C:\ It works when creating the template but when I apply to the same serve it was testing with get the error.

    Sent via the Samsung Galaxy SRegistered 5 ACTIVETm, an AT&T 4G LTE smartphone