2

I am running into issues running subsequent invoke-webrequests.

Say I open the ISE for the first time and run the code below, I get the correct output in a few milliseconds.

Elapsed : 00:00:00.1780360

The See code below.

$timer = [Diagnostics.Stopwatch]::StartNew()
$ProgressPreference = 'SilentlyContinue'
$resp =""
$resp =Invoke-WebRequest -uri 'https://www.canada.ca/en/immigration-refugees-citizenship/news/2023/07/minister-fraser-to-make-an-important-announcement.html' -UseBasicParsing
$ProgressPreference = 'Continue'
$timer.stop
$timer

Can anyone explain to me what is going on ? Is it simply that the initial Webrequest hasn't finished (even though I have the output) ?

I've tried removing the Progress preference as discussed on other threads but to no avail.

However, closing and reopening the ISE will solve the issue without fail.

  • 4
    You are likely hitting some 'anti-scraping' mechanism on the server, as mentioned here: [Invoke webrequest fails to fetch website data even though works in all browsers](https://stackoverflow.com/questions/61225983/invoke-webrequest-fails-to-fetch-website-data-even-though-works-in-all-browsers). As mentioned in the link, mimicking the headers from a normal browser request will likely do the trick. – boxdog Jul 18 '23 at 22:21
  • Brilliant - this worked perfectly. Thanks a million. – Marc Codere Dussault Jul 19 '23 at 15:01

0 Answers0