Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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"}
}
share|improve this question
    
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 Answer 1

up vote 1 down vote accepted

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"}
}
share|improve this answer
    
Thanks. This is does the trick. –  Jake Feb 19 '13 at 12:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.