Search code examples
powershellpowershell-2.0powershell-3.0powershell-remoting

Test path fails to check for folder path


I am trying to check if a specific folders exists in the remote servers.I have file PathList.txt which contains the server address and folder paths to check.But the test path fails even on my local computer(I give my local machine IP).It says path does not exits when the path actually exists

$ServerPaths = Get-Content .\PathList.txt

#Check for paths in servers.
Foreach ($s in $ServerPaths)
{
   $Server,$Paths = $s.split('=',2)
   $AllPaths = $Paths -split ','
   $Server=$Server.Trim()

   Foreach ($Path in $AllPaths)
  {  
     $Path=$Path.Trim()

     $CheckPath = "\\"+$Server+"\"+$Path

     if(Test-Path $CheckPath)
     {
         Write-host $Server $Path "Path exists"
     }
     else
     {
         Write-host $Server $Path "Path does not exists"  
     } 

   }

 }

PathList.txt contains

10.247.211.12 = D$\Install, D$\Dir

Solution

  • I have checked your code and it works fine, but only if you have accessed the path before (so it could cache credentials), or if your current user can access the share. I suggest you to add -Credential parameter to your Test-Path call.