If i run the following code, I get the expected result based on whether the file exists or not with complete output (ie. $filepath always equals "C:\test\test.txt"):
$filepath = "C:\test\test.txt"
If( Test-Path $filepath)
{
Write-Host "File does exist: " $filepath
}
Else
{
Write-Host "File does not exist: " $filepath
}
ie. $filepath is in scope within the if block
Why does this behaviour not seem to translate when reading the filepath from an external file? Here, the conditional always returns false and the output prints as though $fileLocation is empty.
testConfig.xml file:
<ConfigFile>
<FileLocation>C:\test\test.txt</FileLocation>
</ConfigFile>
powershell code:
[xml]$config = Get-Content ".\testConfig.xml"
$fileLocation = $config.SelectNodes("//FileLocation")
If( Test-Path -LiteralPath $fileLocation)
{
Write-Host "File does exist: " $fileLocation
}
Else
{
Write-Host "File does not exist: " $fileLocation
}
Is this something to do with the variable scope? How can I get the same behaviour as the first codeblock when reading the filepath from an xml file?