Ich arbeite an der Erstellung eines Formular-Login-Monitors für eine Webseite. Hier ist der Code:UseBasicParsing gibt kein HTML-Formular zurück
$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;
Das funktioniert ganz gut außerhalb der Monitoring-Plattform, aber wenn ich diese Skript in die Plattform ich die folgende Fehlermeldung erhalten:
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
Nach dem Vorschlag der ersten Fehler, ich habe '-UseBasicParsing' zu meiner ursprünglichen Invoke-WebRequest-Anweisung hinzugefügt. Wenn ich das Skript innerhalb oder außerhalb der Überwachungsplattform ausführe, passiert, dass keine Formulare oder Eingabefelder aus dem HTML-Code abgerufen werden. Ich erhalte die folgende:
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
Ive versucht, eine alternative Lösung auf Google und die Support-Foren unserer Monitoring-Plattform zu finden, aber weder haben sich viel nach oben außerhalb der Standard-Tutorials, wie Invoke-WebRequest zu verwenden.
Wenn Sie eine externe Bibliothek verwenden können, verwenden Sie [HTMLAgilityPack] (http://htmlagilitypack.codeplex.com/). – wOxxOm
@wOxxOm Ich bin ein großer Fan von HTMLAgilityPack, aber die Verwendung von HTMLAgilityPack würde zusätzlichen Code erfordern, um die String- oder Objekttypen zu erhalten, die benötigt werden, um nach 'iwr' zurückzukehren, um sich beim Formular anzumelden. es wäre alles andere als ein Ersatz für das einfache "iwr". Requisiten, wenn Sie diese Antwort hinzufügen möchten :) – briantist