Confirm website login

rated by 0 users
This post has 10 Replies | 2 Followers

Top 150 Contributor
Posts 20
Ironbone Posted: 02-10-2011 10:26 PM

Hi

I want to monitor an intranet site and wrote a loginscript. However I need the script to do something and return a value to confirm that the site loaded.

 

The script so far:

$username ="xxxx";

$password="yyyy";

$url="http://intranet.company.com";

 

$ie= new-object -com internetexplorer.application;

$ie.visible = $true;

$ie.navigate($url);

$ie.Document.getElementById("username").value = $username;

$ie.Document.getElementById("password").value = $password;

$ie.Document.getElementById("login").click();

while ($ie.busy -eq $true)

{

start-sleep -milliseconds 1000;

}

 

 

So now I need some function to confirm that the site loaded, I tried select-string to check for a word on the site but it didn't work.

 

Thanks in advance...

Top 75 Contributor
Posts 51

$webClient = new-object System.Net.WebClient
$data = $webClient.DownloadString($url)

 

 

 

Top 75 Contributor
Posts 51


$ie.navigate($url);

After that code, you do better to have a time to load dom object.
If script try to getElementById before read dom object, error will happen.

so

$ie.navigate($url);

sleep(a few sec) or function(cheek to read object that you need)

$ie.Document.getElementById("username").value = $username;

Top 150 Contributor
Posts 20

The problem with this site is that it allways need authentication so when you run

$webClient = new-object System.Net.WebClient
$data = $webClient.DownloadString($url)

 

You get a 401- unautherized . Even if the script has logged in and loaded the site it calls a new object and need a new authentication. Thats why I need something that can read diectly off the site.

 

I can add that the login is a IE win32 dialogbox

Top 10 Contributor
Posts 637
Microsoft MVP
Top Contributor

when you authenticated with your previous script, then the page is loaded into IE, so why not query for the page content?

$ie.document.body

This returns text, and you can then check if it is what you expected it to be.

Note that IE behaves differently when User Account Control is enabled. It then may change objects based on whether the loaded webpage is Intranet or Internet, and your script may loose control.

Another interesting approach to solve this is to use the old com object Shell.Application:

$shell = New-Object -com Shell.Application

This line accesses any open IE instance:

PS > $shell.Windows() | Where-Object { $_.Document.GetType().Fullname -eq 'mshtml.HTMLDocumentClass' }

This way, you can also automate the content, and this works even for web sites that you manually launched and logged into manually. It does not apply to your login project, but assume there is a webpage that requires complex login, and once logged in you would like to automate things. You could manually open the web page in IE, log in, and then access this instance by script and automate whatever you want.

Top 150 Contributor
Posts 20

Thx for the great reply!

$ie.document.body worked like a charm, I'm trying to find a property(?) that doesn't give me so much data back. My best attemp after some google so far is $ie.document.documentelement.innertext , allthough a way to only get the header or footer would be perfect.

The script works as it is and now ends a long string of attempts! So I thank you all for sharaing your knowledge.

 

/David

 

 

Top 10 Contributor
Posts 637
Microsoft MVP
Top Contributor

Great that it worked!

You should try PowerShellPlus eventually. This IDE gives you intellisense-like help for object properties so it is easy to see what methods and properties any given object provides.

Top 150 Contributor
Posts 20

Holy crap, Powershell Plus is the s**t. All I have to do now is become a MS MVP so I get a license for free :)

 

Thx for the Powershell Plus tip, that is the best tip I've got in ages!

Top 500 Contributor
Posts 11

I'm a newbie Powershell, so this site and your advice are very helpful. I've used your way to access any open IE instance, but I can access an open Chrome or Firefox browser by this way.

PS > $shell.Windows() | Where-Object { $_.Document.GetType().Fullname -eq 'mshtml.HTMLDocumentClass' }

Please tell me know how to do it with Chrome browser. Many thanks!

Top 10 Contributor
Posts 637
Microsoft MVP
Top Contributor

this was answered in another thread. You can only access IE browsers this way.

Not Ranked
Posts 2

Used teh above example to log in to a webpage. Gets so far then a pop up appears asking for "User name" and "Password" with a button "OK" to complete the login.

The line below should be able to populate the "username" in the pop up but is that only if that is the correct element name?

$ie.Document.getElementById("Username").value = $username;

How can the element names of the pop up be determined from a 3rd party site so the fields can be filled in correctly as guessing the elementid is not right?

Making sense? Using ie explorer. Think is a really basic error I'm making.

 

Error message below

You cannot call a method on a null-valued expression.
At C:\powershell\power_ie_test.ps1:19 char:28
+ $ie.Document.getElementById <<<< ("Username").value = $username;
    + CategoryInfo          : InvalidOperation: (getElementById:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

 

 

 

Page 1 of 1 (11 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.