Powershell invoke-webrequest无法正常工作 [英] Powershell invoke-webrequest not working
问题描述
我有一个Powershell脚本,应该对站点运行Invoke-WebRequest
.我想将此脚本与PRTG Network Monitor一起使用,以确保Web服务器已启动并正常运行.
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.
当前,该脚本可以在我的本地计算机上运行,也可以在探测服务器上使用我的域凭据运行.但是,当使用PRTG使用的本地管理员凭据登录时,它不会运行.
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
看来,出于任何原因,使用此帐户时,Invoke-WebRequest
均不起作用.运行该行时,它不会产生任何东西,没有错误,警告或输出. $uri
和$body
变量也已经定义.
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.
即使我使用本地admin PRTG帐户登录了探针服务器,并以其他用户身份(使用我自己的凭据)运行Powershell,它仍然无法产生任何结果.
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.
为什么本地管理员帐户不能运行Invoke-WebRequest
?这是在带有PSVersion 4.0的Windows Server 2012R2上发生的.
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.
推荐答案
[将我的评论变成答案]
[Turning my comment into an answer]
在Windows PowerShell中,除非告知您,否则Invoke-WebRequest
使用Internet Explorer引擎解析网站. IE有很多限制-来自其受信任的站点/安全区域的安全模型,IE增强的安全性,由组策略覆盖的限制,也许还有更多限制,并且您似乎尝试了所有其他明智的尝试,因此值得进行测试直接.
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.
此开关:
Invoke-WebRequest -UseBasicParsing ....
避免使用IE引擎,并发出HTTP请求并在C#/.Net中处理响应.
avoids the IE engine and makes a HTTP request and processes the response all within C#/.Net.
权衡是要获得纯文本结果才能使用,并且无法通过HTML DOM,getElementByTagName()
或需要实时浏览器引擎才能工作的任何内容来访问页面.
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.
这篇关于Powershell invoke-webrequest无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!