Looking for PowerShell scrip for file monitor
Monitoring Logic: Check if the file is generated for today, if not raise an alert.
File name format - Civil Inventory Sheet_<ddmmyyyy>
I have tried below script but not getting correct result
kindly advise
$Path = "\\Path\To\Check"
# Things since midnight today$Today = Get-Date | Select-Object -Property Date | Get-Date$FilesToday = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Today }
# Results for 'today'Write-Host "Message.Today: Found $( $FilesToday.Count ) files in $( $Path ) created on $( $Today.ToString("MM/dd/yyyy") )"Write-Host "Statistic.Today: $( $FilesToday.Count )"
So you're not actually referencing any arguments into your script itself even though you've defined the 'Script Arguments' section with info.
You need to update your first line like this:
$Path = $args[0]
Still same
Have you tried running the script outside of SAM first? It's easier to see potential issues outside of SAM, in a normal powershell session.
We are at least getting the correct path to show in the output. I would definitely follow what @Seashore recommends and run the script natively in PowerShell first. Make sure it works there before testing it in SolarWinds.
There are some more errors in your original code. The $Today line starts with Get-Date, pipes it over to Select-Object and then pipes it again to Get-Date. This is not necessary and it breaks the object-based nature of PowerShell when you attempt to compare it in the $FilesToday line.
The following should get you what you're after.
$Today = (Get-Date).Date
$FilesToday = Get-ChildItem -Path $Path -Recurse -File -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $Today }