新手。ForEach、IF、Else

新手。ForEach、IF、Else

请指教。我正在尝试测试一组计算机桌面上是否存在某个文件。有些计算机是 Windows 7,有些是 Windows XP。当我运行代码时,它不会在每台计算机上测试路径,并且会将每台计算机都返回为“有 URL”,即使我知道文件不存在的计算机也是如此。我不确定我哪里做错了。

$a = Get-Content "C:\Computers.txt"
$windowsXP = "\\$i\c$\Documents and Settings\All Users\Desktop\file.url"
$windows7 = "\\$i\c$\Users\Public\Desktop\file.url" 

ForEach ($i in $a){
 #Test file on WindowsXP
if ((test-path $windowsXP) -eq $True)
    {Write-Output "$i WindowsXP has URL"}

elseif ((test-path $windowsXP) -eq $False)
    {Write-Output "$i WindowsXP needs URL"}

 #Test file on Windows7
elseif((test-path $windows7) -eq $True)
    {Write-Output "$I Windows7 has URL"}

elseif ((test-path $windows7) -eq $False)
   {Write-Output "$I Windows7 needs URL"}
}

答案1

试试这个(未经测试)。尽管它仍然无法解决盲测的事实。

$a = Get-Content "C:\Computers.txt"

ForEach ($i in $a){

$windowsXP = "\\$i\c$\Documents and Settings\All Users\Desktop\file.url"
$windows7 = "\\$i\c$\Users\Public\Desktop\file.url" 

 #Test file on WindowsXP
Write-Output("$i - Checking WindowsXP Location")
if ((test-path $windowsXP) -eq $True)
    {Write-Output "File Found"}

elseif ((test-path $windowsXP) -eq $False)
    {Write-Output "File Not Found "}

 #Test file on Windows7
Write-Output("$i - Checking Windows7 Location")
if((test-path $windows7) -eq $True)
    {Write-Output "File FoundL"}

elseif ((test-path $windows7) -eq $False)
   {Write-Output "File Not Found"}
}

相关内容