Automate logon to webpage


I’m new to learning powershell and picking up the basics just now. I have a an application that runs internally through IE, it is apache based and it coded in HTML. The url is http://localhost/form on this page it has a username field and password field and I want to automate the login then eventually go to a particular page with some menus that control a DLL file and send a value to that DLL.

To start with I just want to get the login automated then work on the other aspects. I’ve viewed the source code from the web page to determine the fields for the username and password fields and how I can pass the credentials to these fields and login. Would anyone be able to point me in the right direction as this is all new to me. I’ve never had to much scripting or work with syntax before.

    <div class="login_PasswordHeading">Password</div>
    <div id="PasswordField" class="login_Password">
	<input type="password" name="password" class="login_TextBox" maxlength="128" value="" tabindex="2"/>

<div class="login_Button" id="btnLogin" onmouseup="SubmitLogin(true);">
    <div class="login_Button_Left">
	<div class="login_Button_Right">
	    <div class="login_Button_Centre">
		<div class="login_Button_Icon"></div>
		<div class="login_ButtonText">Login</div>


Welcome to the forum.

You actually picked an advanced and complex topic to start with. :wink:

In PowerShell we use the cmdlet …

… to interact or to automate the access to web pages. Please read the help completely including the examples to learn how to use it.

Hello yes I’ve already been using the invoke-webrequest cmdlet and been reading about it. I’ve managed to write some lines of code but not entirely successful so far. What I am trying to work out is what are the HTML elements within the source code on the webpage that control the input fields for the username and password controls and how I can get powershell to interact with the these.

I don’t have experiences with that but as far as I understood usually you don’t interact with the controls at all. Take a look at the example #2 of the help. It shows how to provide credentials for a web page.

Yep I’ve looked at that a tried that example to suit what I’m trying to do but don’t achieve what I’m trying to do.

As Olaf stated, this is a bit advanced. The code below was logging into a website to get to a table and parsing it, which was a total pain. To this, I was hitting F12 in the browser and watching the network traffic and trying to emulate it with Powershell and HTML Document Object Model (DOM):

$Uri        = ''
$Username   = 'user'
$Password   = 'password'

#Login page
$evtLogon = Invoke-WebRequest -Uri ('{0}/exhibitor/'-f $Uri) -SessionVariable session

#fill the form body with the username\pw
$formBody = @{username=$Username;password=$Password}

$params = @{
    Method     = 'POST'
    Uri        = ('{0}/exhibitor/'-f $Uri)
    Body       = $formBody 
    WebSession = $session

#attempt login
$evtLoggedOn = Invoke-WebRequest @params

$params.Set_Item('Uri', ('{0}/exhibitor/'-f $Uri))

#go to the page after authentication, find all buttons and click on the 3rd button
$leads = ((Invoke-WebRequest @params).ParsedHTML.getElementsByTagName("button")[2]).Click()

Took a while with trial and error trying to get it to work, but it was working albeit slow. The best way to get data is an API. If they do not have an API, then you are stuck doing something like this.

1 Like

Another option to Rob’s post is to find the ID (view page source or via F12) of the button to push and use that …

$leads = ((Invoke-WebRequest @params).ParsedHTML.getElementById("buttonID").Click()