我正在为网页创建表单登录监视器。这是代码:
$loginbase = '<input omitted>'
$loginURL = $loginbase + '<input omitted>'
$r = Invoke-WebRequest -Uri ($loginURL + 'logonform.jsp')
return $r;
$form = $r.Forms[0];
return $form
$form.Fields['aps'] = '<input omitted>';
$form.Fields['usr'] = '<input omitted>';
$form.Fields['pwd'] = '<input omitted>';
$r = Invoke-WebRequest -Uri ($loginURL + $form.Action) -Method POST -Body $form.Fields;
if ($r.Content -match 'setup()') {
if ($r.StatusCode -eq 200) {
Write-Host 'Message:' $r.StatusDescription;
Write-Host 'Statistic:' $r.StatusCode;
Exit 0;
}
Write-Host 'Message:' $r.StatusDescription;
Write-Host 'Statistic:' $r.StatusCode;
Exit 1;
}
Write-Host 'Message: Did not login';
Write-Host 'Statistic: 1';
Exit 1;
这在监控平台之外工作正常,但是当我将此脚本放入平台时,我收到以下错误:
Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.
At line:3 char:6
+ $r = Invoke-WebRequest -Uri ($loginURL + 'logonform.jsp')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
+ FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Cannot index into a null array.
At line:5 char:1
+ $form = $r.Forms[0];
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:7 char:1
+ $form.Fields['aps'] = '<input omitted>';
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:8 char:1
+ $form.Fields['usr'] = '<input omitted>';
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:9 char:1
+ $form.Fields['pwd'] = '<input omitted>';
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Invoke-WebRequest : The remote server returned an error: (404) Not Found.
At line:11 char:6
+ $r = Invoke-WebRequest -Uri ($loginURL + $form.Action) -Method POST -Body $form. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
根据第一个错误的建议,我将'-UseBasicParsing'添加到我的初始Invoke-WebRequest语句中。当我在监视平台内部或外部运行脚本时,发生的事情是没有从HTML中提取任何表单或输入字段。我得到以下内容:
StatusCode : 200
StatusDescription : OK
Content : <!--
©2010 - 2013 SAP AG or an SAP affiliate company. All rights reserved.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered...
RawContent : HTTP/1.1 200 OK
Content-Length: 2561
Content-Type: text/html;charset=utf-8
Date: Tue, 11 Oct 2016 15:31:12 GMT
Expires: 0
Set-Cookie: JSESSIONID=DE3280200E764687D90E63E50A65452F; Path=/AdminTools...
Forms :
Headers : {[Content-Length, 2561], [Content-Type, text/html;charset=utf-8], [Date, Tue, 11 Oct 2016 15:31:12 GMT], [Expires, 0]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 2561
我试图在谷歌和我们监控平台的支持论坛上找到一个替代解决方案,但是在使用Invoke-WebRequest的标准教程之外都没有做太多改进。
答案 0 :(得分:3)
您尝试使用的功能(即HTML解析为对象模型)要求Internet Explorer可用并初始化。 -UseBasicParsing
具体不会对您不需要它的情况进行解析,并且IE不可用(例如在Server Core上)。
如果您没有运行服务器核心,并且IE实际上可用,那么问题可能是这样的:
Internet Explorer的首次启动配置尚未完成。
在这种情况下,只是第一次运行IE,应该修复它。
如果脚本作为服务帐户运行,请确保使用该帐户登录一次并初始化IE(每个用户)。