0

I wrote a Powershell script where I update hosts file at runtime: add entries, clear entries.

An example of what I am doing:

Import-Module 'Carbon'

#Carbon module functions
Reset-HostsFile

#No exception expected
$result = Invoke-Webrequest -Uri "https://blogs.technet.microsoft.com" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "https://www.google.fr" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "http://www.lequipe.fr" -UseBasicParsing
$result = $null

#Block google.fr
Set-HostsEntry -IPAddress 0.0.0.0 -HostName 'google.fr www.google.fr' -Description "Google"

#No exception expected
$result = Invoke-Webrequest -Uri "https://blogs.technet.microsoft.com" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "https://www.google.fr" -UseBasicParsing
$result = $null

#Exception expected
$result = Invoke-Webrequest -Uri "http://www.lequipe.fr" -UseBasicParsing
$result = $null

#Block lequipe.fr
Set-HostsEntry -IPAddress 0.0.0.0 -HostName 'lequipe.fr www.lequipe.fr' -Description "Google"

#No exception expected
$result = Invoke-Webrequest -Uri "https://blogs.technet.microsoft.com" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "https://www.google.fr" -UseBasicParsing
$result = $null

#Exception expected
$result = Invoke-Webrequest -Uri "http://www.lequipe.fr" -UseBasicParsing
$result = $null

Reset-HostsFile

#No exception expected
$result = Invoke-Webrequest -Uri "https://blogs.technet.microsoft.com" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "https://www.google.fr" -UseBasicParsing
$result = $null

#No exception expected
$result = Invoke-Webrequest -Uri "http://www.lequipe.fr" -UseBasicParsing
$result = $null

When an exception is expected, no exception is thrown. This is because I am still running in the same Powershell environment. If I open a new shell to call Invoke-Webrequest with the uri blocked, an exception is thrown.

Carbon module can be found at this address : http://get-carbon.org/ There is nothing particular with the functions used. Before using Carbon, I was updating hosts file using Powershell cmdlet Clear-Content and Add-Content. The issue was already present.

A solution that I have implemented is to create a Job using the following peace of code:

$job = Start-Job –Scriptblock { Invoke-Webrequest -Uri "https://www.google.fr" -UseBasicParsing } 
Wait-Job $job
Receive-Job $job -OutVariable result
$result

But this solution is not clean, it creates a background worker.

Is there any way to "refresh" powershell environment without closing/opening powershell. I tried:

  • ipconfig /flushdns
  • arp -a
  • netsh interface ip delete arpcache

At the end, the code will run using a Windows Scheduled Task.

Thank you for your support. Vinc3nt

  • Sorry, for the convenience, I use the [System.Net.ServicePointManager]::DnsRefreshTimeout = 0 with DisableKeepAlive property of Invoke-Webrequest and it works. – Vinc3nt Feb 2 '17 at 20:55
  • You should write an answer with your solution and accept it. – Peter Hahndorf Feb 7 '17 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.