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...
$webClient = new-object System.Net.WebClient$data = $webClient.DownloadString($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
sleep(a few sec) or function(cheek to read object that you need)
The problem with this site is that it allways need authentication so when you run
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
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.
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
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.
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!
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.
Please tell me know how to do it with Chrome browser. Many thanks!
this was answered in another thread. You can only access IE browsers this way.
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