Powershell - remote folder availability while counting files
        Posted  
        
            by 
                ziklop
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ziklop
        
        
        
        Published on 2013-11-08T15:26:00Z
        Indexed on 
            2013/11/08
            21:54 UTC
        
        
        Read the original article
        Hit count: 246
        
powershell
|remote-access
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
© Stack Overflow or respective owner