我正在爲網頁創建表單登錄監視器。下面是代碼:UseBasicParsing does not return HTML form
$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
香港專業教育學院試圖找到對谷歌和一個替代的解決方案我們的監控平臺的支持論壇,但也已經把太多瞭如何使用調用-WebRequest的標準教程之外。
如果您可以使用外部庫,然後使用[HTMLAgilityPack](http://htmlagilitypack.codeplex.com/)。 – wOxxOm
@wOxxOm我是HTMLAgilityPack的忠實粉絲,但使用它(或任何其他解析方式)將需要額外的代碼來獲取所需的字符串或對象類型以傳回'iwr'來登錄表單;這將遠遠低於'iwr'這個原生的簡單替代品。道具,如果你想添加該答案:) – briantist