cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Highlighted
Level 7

PowerShell to watch a folder and return file counts

KMSigma provided this script as a starting point, but he's gotten busy with other things.  He suggested that I post it here.  Here's the script:

$Folder = "F:\DuckCreek\Insights.Extract.Server\Errorlog\"

$RemotePath = Join-Path -Path "\\${IP}\" -ChildPath $Folder.Replace(":", "$")

$AllFiles = Get-ChildItem -Path $RemotePath | Where-Object { $_.Extension -in ( ".rq", ".rs" ) } | Sort-Object -Property CreationTime

$LastHour = $AllFiles | Where-Object { $_.CreationTime -ge ( Get-Date ).AddHours(-1) }

Write-Host "Message.LastHourCount: $( $LastHour.Count ) file(s) created in the last hour."

Write-Host "Statistic.LastHourCount: $( $LastHour.Count )"

Write-Host "Message.LastHourOldest: $( $LastHour[0].FullName ) is the oldest file in the folder (from the last hour)."

Write-Host "Statistic.LastHourOldest: $( $LastHour[0].CreationTime.ToString("yyyyMMdd") )"

Write-Host "Message.LastHourOldest: $( $AllFiles[0].FullName ) is the oldest file in the folder (from forever)."

Write-Host "Statistic.LastHourOldest: $( $AllFiles[0].CreationTime.ToString("yyyyMMdd") )"

Here's the results that I need help with understanding what the error is:

When I tried running your template and getting the errors below. Can the community review and help?  The overall script/template just needs to run once an hour.

Output: ==============================================
Message.LastHourCount: 0 file(s) created in the last hour.
Statistic.LastHourCount: 0
Message.LastHourOldest: is the oldest file in the folder (from the last hour).
Statistic.LastHourOldest:
Message.LastHourOldest: is the oldest file in the folder (from forever).
Statistic.LastHourOldest:

Errors: ==============================================
Cannot index into a null array.
At line:12 char:40
+ Write-Host "Message.LastHourOldest: $( $LastHour[0].FullName ) is the ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At line:13 char:42
+ ... .LastHourOldest: $( $LastHour[0].CreationTime.ToString("yyyyMMdd") )"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At line:15 char:40
+ Write-Host "Message.LastHourOldest: $( $AllFiles[0].FullName ) is the ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At line:16 char:42
+ ... .LastHourOldest: $( $AllFiles[0].CreationTime.ToString("yyyyMMdd") )"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

0 Kudos
3 Replies
Highlighted
Level 12

Re: PowerShell to watch a folder and return file counts

It's throwing the error because the query for files older than one hour returned no results. This means that $lasthour is null (or empty), causing the error to come up. It looks like the same applies to $allFiles as well.

There could be a couple of causes for this. The first is a permissions issue.  I noticed in the script that it is attempting to access the remote fie server using the special admin share F$.  Do you specify the check to run as a specific account in the check properties? Are you able to run that powershell script as that same account from the server running Solarwinds?

0 Kudos
Highlighted
Level 7

Re: PowerShell to watch a folder and return file counts

thank you....I don't believe I can use network shares....can i use the full directory name instead of the f$ to make it work? Appreciate your feedback.

0 Kudos
Highlighted
Level 12

Re: PowerShell to watch a folder and return file counts

Why would you not be able to use a network share? How are you planning on executing the checks? Are the checks being executed against the SolarWinds server itself?  You can change the first two lines so it says this instead to run on the local drive:

$remotePath = "F:\DuckCreek\Insights.Extract.Server\Errorlog\"
0 Kudos