Newbie. ForEach, IF, Else
6,922
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 videos on Youtube
Author by
Jake
Updated on September 18, 2022Comments
-
Jake almost 2 years
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"} }
-
Dan over 11 yearsI 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.
-
Jake over 11 yearsThe file is the same on XP and 7, just the location is different.
-
Dan over 11 yearsAlso, 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 over 11 yearsYeah, that's what I mean
-