为什么从Invoke-WebRequest的结果中访问某些属性会打开我的浏览器?

时间:2015-03-20 17:42:41

标签: powershell powershell-v3.0

当访问我们公司内部网络中通过WIF passive authentication保护的网址时,powershell决定打开我的默认网络浏览器,并导航到我的iwr命令中指定的网址,当我访问结果的某些属性时。

当我将目标指向像google.com这样的目标时,就不会发生这种情况。

任何人都可以解释这种(疯狂的)行为,以及我如何阻止它?

代码示例

$response = (Invoke-WebRequest 'http://myhost.example.com/MyApp/' -UseDefaultCredentials)
write-host $response.GetType()
write-host $response.StatusCode
$response.Forms[0] # causes browser to open, but does return the content!
# comment out the above line, and no browser opens

输出

>Microsoft.PowerShell.Commands.HtmlWebResponseObject
>200
>
>Id              Method  ...
>hiddenform      post    ...

Fiddler HTTP摘要

GET  302  http   myhost.example.com   /MyApp/           powershell_ise
GET  401  https  sts.example.com      /default.aspx?... powershell_ise
GET  401  https  sts.example.com      /default.aspx?... powershell_ise
GET  200  https  sts.example.com      /default.aspx?... powershell_ise
GET  302  http   myhost.example.com   /MyApp/           chrome
GET  401  https  sts.example.com      /default.aspx?... chrome
GET  401  https  sts.example.com      /default.aspx?... chrome
GET  200  https  sts.example.com      /default.aspx?... chrome
POST 200  http   myhost.example.com   /MyApp/           chrome
GET  200  http   myhost.example.com   /MyApp/           chrome

1 个答案:

答案 0 :(得分:2)

我有类似行为的问题。到目前为止我发现的内容可能会帮助您缩小范围。

tl; dr - 为请求添加选项-UseBasicParsing

我有一个受SAML保护的Web服务,该服务使用http 302重定向来提供登录表单。我的脚本遵循重定向,提交表单(有三个),最终得到针对原始服务URI的休息请求。

只有一个步骤导致浏览器打开 - 当身份验证服务在html页面中使用预先填写的表单进行响应时,顶部有一些onload(submit())脚本。正确的行为是立即提交表格。

导致浏览器打开的代码片段是

$results.Forms["Response"].Fields["LARES"]

我可以访问响应的任何其他部分,包括内容(包括原始html中的表单),但任何评估LARES值或包含它的表单的尝试都会导致浏览器打开完全限定的URI表单的ACTION属性。

解决这个问题,并且仍然掌握数据的方法是关闭强制Windows使用IE浏览器参与的DOM解析。将选项-UseBasicParsing添加到Invoke-WebRequest可防止浏览器参与其中。这意味着不会填写Forms []数组,但InputFields []数组仍然可用。

所以,而不是访问

$result.Forms["Response"].Fields["LARES"]

我现在访问

$result.InputFields[0].VALUE

其中$ result.InputFields [0] .NAME是“LARES”。

相关问题