2016-10-11 72 views
1

我正在爲網頁創建表單登錄監視器。下面是代碼: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的標準教程之外。

+0

如果您可以使用外部庫,然後使用[HTMLAgilityPack](http://htmlagilitypack.codeplex.com/)。 – wOxxOm

+0

@wOxxOm我是HTMLAgilityPack的忠實粉絲,但使用它(或任何其他解析方式)將需要額外的代碼來獲取所需的字符串或對象類型以傳回'iwr'來登錄表單;這將遠遠低於'iwr'這個原生的簡單替代品。道具,如果你想添加該答案:) – briantist

回答

2

您嘗試使用的功能(將HTML解析爲對象模型)需要Internet Explorer可用並已初始化。 -UseBasicParsing明確不會解析,你不需要它的情況下,IE不可用(如在服務器核心上)。

如果沒有運行服務器核心,IE實際上是可用的,那麼問題可能是這樣的:

Internet Explorer's first-launch configuration is not complete.

在這種情況下,只要運行IE瀏覽器的第一次,這應該修復它。

如果腳本作爲服務帳戶運行,請確保您使用該帳戶登錄一次並初始化IE(每個用戶)。

+0

嗯,我沒有想到這一點,所以我確保IE的首次啓動配置完成(它沒有)的服務帳戶。我仍然遇到錯誤,但我將回到產品的支持論壇,看看他們有什麼要說的。 – Resin

+0

@Resin我還發現[此頁](http://wahlnetwork.com/2015/11/17/solving-the-first-launch-configuration-error-with-owowhells-invoke-webrequest-cmdlet/),它使用組策略禁用首次運行。這可能會更好。我還隱約記得一個錯誤,在某些情況下,錯誤在第一次運行配置後仍然存在,但我現在找不到任何對此的引用。我還補充說,在完成IE第一次運行之後,再次嘗試'iwr'之前,應該完全關閉並重新打開powershell。 – briantist

+0

所以我想回來讓每個人都知道解決方案是什麼,以防一些可憐的靈魂最終讀到這個線索的解決方案。 監控解決方案作爲本地系統運行其服務。所以我不得不對IE做一個註冊表編輯,以在計算機配置>管理模板> Windows組件> Internet Explorer下「阻止運行第一次運行嚮導」。 我必須將其設置爲啓用,然後選擇其中一個選項(我選擇了第二個選項)。 – Resin