0

I´m trying to make a Powershell script that reports if there´s a file older than x minutes on remote folder. I make this:

$strfolder = 'folder1 ..................'
$pocet = (Get-ChildItem \\server1\edi1\folder1\*.* ) | where-object {($_.LastWriteTime -lt      (Get-Date).AddDays(-0).AddHours(-0).AddMinutes(-20))}   | Measure-Object
if($pocet.count -eq 0){Write-Host $strfolder "OK" -foreground Green}
else {Write-Host $strfolder "ERROR" -foreground Red}

But there´s one huge problem. The folder is often unavailable for me bacause of the high load and I found out when there is no connection it doesn´t report an error but continues with zero in $pocet.count. It means it reports everything is ok when the folder is unavailable.

I was thinking about using if(Test-Path..) but what about it became unavailable just after passing Test-Path?

Does anyone has a solution please?

Thank you in advance

ziklop
  • 15
  • 3
  • I'm not sure why you don't get an error. If I run "Get-ChildItem \\server1\edi1\folder1" (which doesn't exist) I get an error and this is normal, expected behaviour. Is this your complete script? What is the value of your $ErrorActionPreference ? If it is set to "SilentlyContinue" it may be hiding this error. – Robin Nov 08 '13 at 15:39
  • Thanks for reply. Yes, this is all at this time. I´ve started with Powershell in about 8 hours ago to simplify my work and I haven´t learn about $ErrorActionPreference yet. I also expected an error, but I tried this script on other computers with no access to remote folder even with fake paths and it writes me OK. So, now I´m not sure to use it... – ziklop Nov 08 '13 at 16:07
  • I found the reason why it wasn't erroring and have proposed an answer below for you. Please mark it as the answer if it helps! – Robin Nov 08 '13 at 16:25
  • A few tips: the parens around Get-ChildItem are unnecessary and for the date (Get-Date).AddMinute(-20) is sufficient. – Keith Hill Nov 08 '13 at 16:50
  • @KeithHill I must say I didn't even read that bit (bad form!). I've cleaned up the code based on this. Thank you. – Robin Nov 08 '13 at 17:10

1 Answers1

1

I tried this line of your code and experienced the same result (no error even when the path doesn't exist) even with the default $ErrorActionPreference set to Continue.

$pocet = (Get-ChildItem \\server1\edi1\folder1\*.* ) | where-object {($_.LastWriteTime -lt (Get-Date).AddDays(-0).AddHours(-0).AddMinutes(-20))} | Measure-Object

Try this instead (removing the *.* makes it error, but we can put that back in the -filter parameter):

$pocet = (Get-ChildItem \\server1\edi1\folder1 -Filter *.* ) | where-object {($_.LastWriteTime -lt (Get-Date).AddDays(-0).AddHours(-0).AddMinutes(-20))} | Measure-Object

However $pocet.Count will still equal 0 because you've essentially created an object of the type Microsoft.PowerShell.Commands.MeasureInfo which exists in $pocet, and the count property is going to equal zero when nothing is passed to it.

Instead I'd try this:

try
{
    $pocet = Get-ChildItem "\\server1\edi1\folder1" -Filter *.* -ErrorAction Stop | where-object { ($_.LastWriteTime -lt (Get-Date).AddMinutes(-20)) }
    if(($pocet | Measure-Object).Count -eq 0)
    {
        Write-Output "Folder ok"
    }
    else
    {
    }
}
catch
{
    Write-Output "Error getting items from folder"
    Write-Output $Error[0].Exception.Message
}
Robin
  • 1,602
  • 3
  • 16
  • 24
  • FYI `@($pocet).Count -eq 0` will do on V1/V2 and on >= V3 you can simply do `$pocet.Count -eq 0`. – Keith Hill Nov 08 '13 at 16:53
  • Very good idea, but I´ve tried it now and got error and OK -> The catch part doesn´t start even when the forlder path doesn´t exist – ziklop Nov 08 '13 at 18:15
  • I've forced the error generated by Get-ChildItem to be a terminating error by adding -ErrorAction Stop. A terminating error is required in order for the error to be caught. Please try now :) – Robin Nov 08 '13 at 21:51