PowerShell 5.1 on Win10 Pro, from home, on Comcast.
I want to get my external IP.
My first request gets 403 Forbidden.
But then i just repeat the request (using the keyboard UpArrow), and it works - i get my IP as expected.
But if i pause for awhile (several minutes) i'll get the 403 again, and then it works
PS> (Invoke-WebRequest -uri "icanhazip.com").Content
Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
At line:1 char:2
+ (Invoke-WebRequest -uri "icanhazip.com").Content
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS> (Invoke-WebRequest -uri "icanhazip.com").Content
xx.xx.xx.xx (obscured, but IP as expected)
I can put the WebRequest line into a file and invoke it, and it fails first time, works second time.
I can put the WebRequest line twice into a file, and invoke it - it fails once then then works, just like at the command line. I have not had this problem with PS 7, which makes it seem like a PS5 problem. But i also don't get the issue when i use "https://wtfismyip.com/text", which makes it seem like a icanhazip.com problem.
The default method of Invoke-WebRequest is 'Get'. Specify the method as 'Post' and the error is not returned.
$response = (Invoke-WebRequest -uri "icanhazip.com" -method Post).Content