-1

Apologies, I'm incredibly new to PowerShell (And scripting in general), and I'm having a problem with the very basics of IE Automation that I can't quite get my head round. What I want to do is have a script that automatically logs onto a webpage, and then inputs data into a form. But I can't seem to input data into the text input fields on the login page. I've been scouring the internet left right and centre, but haven't yet found the answer, though I imagine it will be an obvious one.

Here is my script so far:

$ie = new-object -ComObject InternetExplorer.Application;

$requestUri = "www.testurl.com"


$ie.visible = $true
$ie.navigate($requestUri)
while ($ie.ReadyState -ne 4)
{ 
    start-sleep -Seconds 1; 
}

$doc = $ie.Document
$doc.GetElementById("ppm_login_username") = $userName
$userName.value = "UserName"

However, whenever I run the script, I get the error

The property 'value' cannot be found on this object. Verify that the property exists and can be set.
+ $userName.value = "UserName"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

I have no experience with a lot of this, so again, apologies for using incorrect terminology. Using the DOM explorer, the input field has the following line of code:

<INPUT id=ppm_login_username maxLength=240 size=40 name=userName>

So I'm confident that I'm getting the correct object, but it doesn't seem to accept the 'value' method when trying to pass data through to it.

Value does show up as a property of the object though, so I can't understand why it doesn't pass through. Any help and time you can offer is greatly appreciated!

HMNIW
  • 29
  • 4

2 Answers2

0

Where is the variable $userName defined? It needs to have the property "value".

Usually you could add it like this:

$Username | Add-Member -MemberType NoteProperty -Value "UserName" -Name value

but I am not sure whether this is possible for a variable of the type string.

I am not sure why you are trying to set $userName.value = "UserName" anyway.

Additionally it might be better to just use Invoke-WebRequest / Invoke-RestMethod, instead of trying to automate IE :)

https://technet.microsoft.com/de-de/library/hh849901.aspx

https://technet.microsoft.com/en-us/library/hh849971(v=wps.620).aspx

whatever
  • 871
  • 4
  • 11
  • 1
    Have you ever logged in to a website using form authentication with `Invoke-WebRequest`? `Invoke-WebRequest` works if you know the target and it has ex. basic authentication, but form auth. is something else. :-) – Frode F. Jun 01 '16 at 15:03
  • I'll openly admit to not having used Invoke-WebRequest a lot, but I have used it to automatically log into a mantis instance which should qualify as form authentication? Anyway, it was only meant as maye a possible alternative without really thinking about it :) Did you manage to fix your original issue? – whatever Jun 02 '16 at 10:27
0

You've mixed it up. It should be $variable = value. Try:

$doc = $ie.Document
#Set $username to reference of "ppm_logon_username"-input node
$userName = $doc.GetElementById("ppm_login_username")
$userName.value = "UserName"
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • Thank you so much Frode. Such a silly error, but it makes so much sense when explained! – HMNIW Jun 02 '16 at 12:15