Without testing it, I'd guess it's dns caching.
The powershell-session probably caches the ip on the first request and ignores your hosts-file update(just uses the old working ip).
Try running your script before and after disconnecting your network adapter/cable to simulate server failure.
UPDATE: What I'm trying to say above is that the script would work perfectly if the servers is unavailable, but your simulation using the hosts-file is giving a "false-positive" (so ignore the results).
If you really need to test the script with editing the hosts file, disable .Net dns cache in the session by adding the following line at the start of your script:
[System.Net.ServicePointManager]::DnsRefreshTimeout = 0