samedi 17 décembre 2016

Powershell scripts assigns a null value to a variable

I've looked everywhere and can't find a solution. I wrote a script to check the result of submitting a string to a web page. I then check if the resulting string contains a certain substring and the script should act accordingly with an if loop. For some reason, the variable that's suppose to contain the string is equal to $null. The weird part is - this ONLY happens when I run the script as a script, but not when I run the specific command line in the ISE. The relevant part of the code is:

$Result = $($objIE.Document.getElementById("JSONResultDiv")).innertext
if ($Result -like "*true*")
{
    Write-Host "True" 
}
else
{
    Write-Host "False"
}

I tried using Set-PSDebug -Trace 2 and it showed me the statement comes out true, yet the script still executed the false clause(?!) Moreover, if I put write-host $result after defining the variable it works Same with a breakpoint, if I set a breakpoint just after checking the statement and before entering the if loop itself, it shows that it defined the variable. I feel confused, what am I missing?

Thanks in advance for helping.

Aucun commentaire:

Enregistrer un commentaire