Powershell – Newbie. ForEach, IF, Else

powershellpowershell-v3.0

Please educate me. I am trying to test if a file exists on a group of computers' desktop. Some computers are Windows 7 and some are Windows XP.
When I run the code, It doesn't test the path on every computer and returns every computer as "has URL" even computers where I know the file doesn't exist. I'm not sure where I have gone wrong.

$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"}
}

Best Answer

Try this (Untested). Though it still doesn't get around the fact that you're testing blind.

$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"}
}
Related Topic