Invoke-WebRequest -Login

This topic contains 5 replies, has 2 voices, and was last updated by  Curtis Smith 7 months ago.

  • Author
  • #77674

    Wei-Yen Tan

    I am trying to loginto a web application called Thruk.. it is using the OMD version.

    I am using invoke-webrequest to use a websession to try and connect to a local thruk omd instance. I am trying to correlate the forms the best i can from the examples but when I try and submit the web session the output just keeps going back to the login page.

    This is the following code I run:

    add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
                return true;
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    $login = Invoke-WebRequest "https://icinga/bcmonitoring/thruk/cgi-bin/login.cgi?bcmonitoring/thruk/" -SessionVariable fb
    $login.Forms[0].Fields.loginuser = $cred.UserName
    $login.Forms[0].Fields.password = $cred.GetNetworkCredential().Password
    $UAstring = "Firefox"
    $mainPage = Invoke-WebRequest ("https://icinga/bcmonitoring/thruk/cgi-bin/login.cgi?bcmonitoring/thruk/") -WebSession $fb -Body $login -Method Post -UserAgent $UAstring 

    This is the output:

    The above is just an excerpt. Any help will be most appreciated...

  • #77676

    Curtis Smith

    Here's what I came up with using the Thrunk Demo page.

    #Initialize Session
    $R=Invoke-WebRequest -SessionVariable thrunksession
    #Set Login and Password
    $R=Invoke-WebRequest -Uri ("" + $R.Forms[0].Action) -WebSession $thrunksession -Method POST -Body $R.Forms[0].Fields
    $R=Invoke-WebRequest -Uri ("") -WebSession $thrunksession -Method POST
  • #77683

    Wei-Yen Tan

    Thanks @Curtis!

    I compared and contrasted your code and saw that the difference that you had was


    where I put it as loginuser

    Thats because that is what i see in the output:

    Id Method Action    Fields                                                     
    -- ------ ------    ------                                                     
       post   login.cgi {[referer, ], [loginuser, ], [password, ], [submit, Login]}

    I am curious why is that powershell understood login instead of login user. Is this how it is designed?

  • #77691

    Curtis Smith

    loginuser is the ID of the field, but the name of the filed is login.

  • #77692

    Wei-Yen Tan

    Oh, so i am assuming you were looking at the inputfields property?

    innerHTML : 
    innerText : 
    outerHTML : 
    outerText : 
    tagName   : INPUT
    id        : loginuser
    class     : logininput
    name      : login
  • #77709

    Curtis Smith

    I actually used the developer tools of IE and/or Chrome to look at the form html on the page.

You must be logged in to reply to this topic.