Powershell login.

by mxn at 2012-10-04 01:31:13

Morning lads,

I’m doing a workflow to check the server status in order to that I need to write a script that will open a IE8 and logon to a specific webpage. I’ve got some code already but it’s throwing me a error, I’ve googled it but can’t find any particular reason why is this happening. Does anyone got some ideas why? Below example of the code:


$url = "http://www.somewebpage.com"
$ie = New-Object -comobject InternetExplorer.Application
$ie.visible = $true
$ie.silent = $true
$ie.Navigate($url)
while( $ie.busy){Start-Sleep 1}
$ie.Document.getElementsByType("loginField").value = "user"
$ie.Document.getElementsByName("input").value = "password"
$loginBtn = $ie.Document.getElementsById(‘input’) | Where-Object {$.Type -eq ‘button’ -and $.Value -eq ‘LoginButton’}
$loginBtn.click()

Error message I get is unfortunately in french as it’s on french server and here’s what I’ve got:
Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
Au niveau de ligne : 1 Caractère : 31
+ $ie.Document.getElementsByType <<<< ("loginField").value = "user"
+ CategoryInfo : InvalidOperation: (getElementsByType:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

PS D:\test> $ie.Document.getElementsByName("input").value = "password"
Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
Au niveau de ligne : 1 Caractère : 31
+ $ie.Document.getElementsByName <<<< ("input").value = "password"
+ CategoryInfo : InvalidOperation: (getElementsByName:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

PS D:\test> $loginBtn = $ie.Document.getElementsById(‘input’) | Where-Object {$.Type -eq ‘button’ -and $.Value -eq ‘Lo
ginButton’}
Vous ne pouvez pas appeler de méthode sur une expression ayant la valeur Null.
Au niveau de ligne : 1 Caractère : 41
+ $loginBtn = $ie.Document.getElementsById <<<< (‘input’) | Where-Object {$.Type -eq ‘button’ -and $.Value -eq ‘Login
Button’}
+ CategoryInfo : InvalidOperation: (getElementsById:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull


just for this topic ID were changed, worth mentioning if fact that I’m complete rookie if it comes to powershell.
PS v2.0 by the way.

Many thanks,
by poshoholic at 2012-10-04 06:50:47
The error messages are telling you that after your while loop, $ie.Document is null. You can’t invoke a method on a null object because the object does not exist.

Here are some troubleshooting steps you can take:

1. Comment out all lines below the while statement so that you are simply loading the document.
2. Run the script. Wait until you see the document appear in the IE window that shows up. Once that appears, look at $ie.Document in PowerShell and make sure it is not null.
3. If the page loads properly and if $ie.Document isn’t null, add a delay between the while statement and the line after the while statement by invoking Start-Sleep with a short delay. Or add a second while statement, something like this:
$counter = 0
while (($counter -lt 10) -and ($ie.Document -eq $null)) {Start-Sleep 1; $counter++}

That will give IE up to 10 additional seconds for the document object to load before trying to do something with the document object.

Lastly, don’t forget to invoke $ie.Quit() at the end of your script if you’re automating something that is expected to run unattended. Otherwise you end up creating a bunch of IE processes that are left open.
by Lecter at 2012-10-16 02:46:05
Hi

I’m working on similar issue:

Got the code


$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("http://rion/browse/login")
$ie.visible = $true

$doc = $ie.document
$user = $doc.getElementById("A675041931")
$password = $doc.getElementById("A-1296592935")
$submit = $doc.getElementById("Submit")

$user.value = "yourUserName"
$password.value = "yourPassword"
$submit.Click();

$ie.Quit();


And have some error:

[quote]
PS U:&gt; $user.value = "yourUserName"
Property ‘value’ cannot be found on this object; make sure it exists and is settable.
At line:1 char:7
+ $user. <<<< value = "yourUserName"
+ CategoryInfo : InvalidOperation: (value:String) , RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

PS U:&gt; $password.value = "yourPassword"
Property ‘value’ cannot be found on this object; make sure it exists and is settable.
At line:1 char:11
+ $password. <<<< value = "yourPassword"
+ CategoryInfo : InvalidOperation: (value:String) , RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

PS U:&gt; $submit.Click();
You cannot call a method on a null-valued expression.
At line:1 char:14
+ $submit.Click <<<< ();
+ CategoryInfo : InvalidOperation: (Click:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
[/quote]

Could you investigate with me this problem ?
by Lecter at 2012-10-16 03:13:18
I’ve started with something simplier. I’ve started to learn PS. Found some code:
So inserting "string" in google and submit


$IE= new-object -com InternetExplorer.Application
$IE.navigate2(“http://www.google.com”)
while ($IE.busy) {
sleep -milliseconds 50
}
$IE.visible=$true
$IE.Document.getElementById(“q”).value=”PowerShell”
$IE.Document.getElementById(“f”).submit()


what exactly do and what it’s from:

[quote]getElementById(“q”)[/quote]

?
by nohandle at 2012-10-16 03:39:56
On the google page "q" is the edit where you put your question.

What exactly are you interesed in? How to find out the ID of an element on any page?
by Lecter at 2012-10-16 03:47:02
HI

Thanks for answer.

I would like to open web page, fill user name and password and click login


$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate("http://rion/browse/login")
$ie.visible = $true

$doc = $ie.document
$user = $doc.getElementById("A675041931")
$password = $doc.getElementById("A-1296592935")
$submit = $doc.getElementById("Submit")

$user.value = "yourUserName"
$password.value = "yourPassword"
$submit.Click();

$ie.Quit();


But the problem is with

[quote]
PS U:&gt; $user.value = "yourUserName"
Property ‘value’ cannot be found on this object; make sure it exists and is settable.
At line:1 char:7
+ $user. <<<< value = "yourUserName"
+ CategoryInfo : InvalidOperation: (value:String) , RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

PS U:&gt; $password.value = "yourPassword"
Property ‘value’ cannot be found on this object; make sure it exists and is settable.
At line:1 char:11
+ $password. <<<< value = "yourPassword"
+ CategoryInfo : InvalidOperation: (value:String) , RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

PS U:&gt; $submit.Click();
You cannot call a method on a null-valued expression.
At line:1 char:14
+ $submit.Click <<<< ();
+ CategoryInfo : InvalidOperation: (Click:String) , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
[/quote]

But As I see problem is with getElementById
How to use it? how to get proper value of getElementById

Thanks for any clue