Controlling Internet Explorer object from PowerShell

Controlling Internet Explorer object from PowerShell

  • Comments 10

NOTE: The site must be in the Trusted Sites Zone for this script to work - James Brundage [MSFT] January 5th, 2009

The following example shows how to display all the processes in an IE window and highlight the ones with memory usage of greater than or equal to the specified value.


$oIE=new-object -com internetexplorer.application
$oIE.navigate2("About:blank")
while ($oIE.busy) {
    sleep -milliseconds 50
}
$oIE.visible=$true
$procList=ps |select-object ProcessName,Handles,NPM,PM,WS,VM,CPU,Id |convertto-html

$oDocBody=$oIE.document.documentelement.lastchild ;

#populate the document.body
$oDocBody.innerhtml=$procList

$oDocBody.style.font="12pt Arial";
$oIE.document.bgcolor="#D7D7EA"

#Reading back from IE.
$oTBody=@($oIE.document.getElementsByTagName("TBODY"))[0] ;
foreach ($oRow in $oTBody.childNodes)
{
   #check the 4 column(WS),and highlight it if it is greater than 5MB.
   $WS=(@($oRow.childNodes)[4].innerhtml) -as [int]  ;
   if (($ws -ne $null) -and ($WS -ge 5mb)) {
       $oRow.bgColor="#AAAAAA" ;
   }
}

#Prepare a title.
$oTitle=$oIE.document.createElement("P")
$oTitle.style.font="bold 20pt Arial"
$oTitle.innerhtml="Process List";
$oTitle.align="center" ;

#Display the title before the Table object.
$oTable=@($oIE.document.getElementsByTagName("TABLE"))[0] ;
$oDocBody.insertBefore($oTitle,$oTable) > $null;


Displaying the “$procList”  can also be accomplished with “write” methods instead of innerhtml assignment.  But we should perform some extra checks to determine  whether the document.body  is type of [mshtml.htmldocumentclass]. If the “htmlfile” progid has the following settings in the registry:


HKEY_CLASSES_ROOT\CLSID\{25336920-03F9-11CF-8FD0-00AA00686F13}\InProcServer32 

Class        : mshtml.HTMLDocumentClass
Assembly  : Microsoft.mshtml, Version=7.0.3300.0,  Culture=neutral,       PublicKeyToken=b03f5f7f11d50a3a


then, mshtml.htmldocumentclass become .NET wrapper for the document.body object.
So the following line :
$oDocBody.innerhtml=$procList

Can be replaced with:

If ($oIE.document.psbase.tostring() –eq “system.__comobject”) {
    $oIE.document.write([string]$proclist)
}
else {
    $oIE.document.IHTMLDOcument2_write([string]$proclist)
}
$oDocBody=$oIE.document.documentelement.lastchild ;

 

Yuksel Akinci [MSFT]
Windows PowerShell Team
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

 

Leave a Comment
  • Please add 5 and 5 and type the answer here:
  • Post
  • Is it possilbe to create additional tabs in the same browser window, for additional or differnet information about the same request?
  • I get this error:

    >> {
    >>    #check the 4 column(WS),and highlight it if it is greater than 5MB.
    >>    $WS=(@($oRow.childNodes)[4].innerhtml) -as [int]  ;
    >>    if (($ws -ne $null) -and ($WS -ge 5mb)) {
    >>        $oRow.bgColor="#AAAAAA" ;
    >>    }
    >> }
    >>
    Bad numeric constant: 5mb.


    I changed it to 5*1024*1024 and it seems to be good now.
  • you can just drop "b" in "5mb" and everything will work fine.
  • ActiveMode: I don't believe IE exposes their tab management through their COM scripting API.

    Lee
  • It is possible to control IE Print Setting (Internet - Options - Printing - Print background colors and images) from PowerShell? Thanks.
  • Inspired by tis article, I'm trying without success to simply access form powershell to elements of an html page, here's the code:

    $oIE=new-object -com internetexplorer.application

    $oie.navigate2("http://www.google.com")

    $oie.document.body.childnodes

    format-default : La référence d'objet n'est pas définie à une instance d'un objet.

    (sorry for the french error message, I do not know to make powershell speaks english everytime)

  • Is it possible to control Pop-up Blockers using PowerShell?

  • Would it be possible to measure the amount of time that it takes to download "www.google.com" and its objects? (jpgs, and/or gifs)

    Thanks,

    Rick

  • How to access website using login and password credentials with Powershell

    email dnagpaul@kpmg.com

  • How to access website using login and password credentials with Powershell

    email dnagpaul@kpmg.com

    Thanks!

Page 1 of 1 (10 items)