Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell invoke-webrequest not working

I have a Powershell script that is supposed to run an Invoke-WebRequest against a site. I want to use this script with PRTG Network Monitor to ensure that a web server is up and running properly.

Currently, the script works running from my local machine as well as running with my domain credentials on the probe server. However, it doesn't run when logged in with the local admin credentials which is what PRTG uses.

I've narrowed it down to a single line,

Invoke-WebRequest $uri -Method POST -Body $body -TimeoutSec 10

It appears that for whatever reason, when using this account, Invoke-WebRequest will not work. When running that line, it doesn't produce anything, no error, warning, or output. Both $uri and $body variables are already defined as well.

Even if I'm logged into the probe server with the local admin PRTG account and run Powershell as another user (using my own credentials) it still fails to produce anything.

Why would a local admin account not be able to run Invoke-WebRequest? This is happening on a Windows Server 2012R2 with PSVersion 4.0.

like image 402
Shawn L. Avatar asked May 09 '18 17:05

Shawn L.


People also ask

Does invoke-WebRequest use proxy?

Beginning in PowerShell 7.0, Invoke-WebRequest supports proxy configuration defined by environment variables.

What is the difference between invoke RestMethod and invoke-WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.


1 Answers

[Turning my comment into an answer]

In Windows PowerShell, Invoke-WebRequest uses the Internet Explorer engine for parsing websites, unless you tell it not to. IE has a lot of restrictions - coming from its security model of Trusted Sites/security zones, IE Enhanced Security, overrides by Group Policy, maybe more, and it looks like you'd tried everything else sensible, and it was worth trying to test that directly.

This switch:

Invoke-WebRequest -UseBasicParsing ....

avoids the IE engine and makes a HTTP request and processes the response all within C#/.Net.

The trade-off is that you get plain text results to work with and can't access the page through the HTML DOM, no getElementByTagName() or anything that needs a live browser engine to work.

like image 163
TessellatingHeckler Avatar answered Oct 08 '22 04:10

TessellatingHeckler