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"}
}
Jake
  • 2,268
  • 5
  • 30
  • 40
  • I don't think you're going about this the right way - if you're testing for a file that's different in XP and 7 then you really need to have two lists or do some prior processing to differentiate. Currently, you have no way of telling the difference between, for example, a Windows 7 machine that doesn't have the path and a Windows XP machine that does. – Dan Feb 19 '13 at 11:03
  • The file is the same on XP and 7, just the location is different. – Jake Feb 19 '13 at 11:06
  • Also, I don't know Powershell but I'm pretty sure you can't pass a variable into a string that way. Move your $windows[xx] definitions to just after the `ForEach` – Dan Feb 19 '13 at 11:06
  • Yeah, that's what I mean – Dan Feb 19 '13 at 11:07

1 Answers1

2

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"}
}
Dan
  • 15,430
  • 1
  • 36
  • 67