测试路径无法检查文件夹路径

时间:2016-05-12 10:31:14

标签: powershell powershell-v2.0 powershell-v3.0 powershell-remoting

我正在尝试检查远程服务器中是否存在特定文件夹。我有文件PathList.txt,其中包含要检查的服务器地址和文件夹路径。但是,即使在我的本地计算机上,测试路径也会失败(我给了我的本地机器IP)。当路径实际存在时,路径不会退出

$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包含

10.247.211.12 = D$\Install, D$\Dir

1 个答案:

答案 0 :(得分:1)

我已经检查了你的代码并且它工作正常,但前提是你之前已经访问过路径(因此它可以缓存凭据),或者当前用户可以访问共享。我建议您在-Credential电话中添加Test-Path参数。

相关问题