In some cases we need to login on multiple web based management portals. Of course we could do this all manually if it’s only a few systems we need to check this way. But when it’s over a dozen, it becomes a tedious task of opening a new tab, filling in the username and password and in worst case you have to deal with a certificate warning as well…

At one of our customers, we need to follow a checklist to check the individual iDRAC web interfaces for issues… for over 32 different systems. I don’t have to tell you that this is a very boring procedure. So I started wondering on how we could automate this task using powershell.

In this first part, we will be going over the basics of IE automation with powershell.

Using the COM objects

The example uses COM objects to start Internet Explorer and manipulate the pages in the browser.

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application

To make sure we can actually see our browser, set the visible property to true.

$objInternetExplorer.visible = $true

Now, let’s try and open a webpage.

$objInternetExplorer.navigate("https://www.google.com")

This should open Google in a new Internet Explorer window. These 3 commands form the base to automate Internet Explorer via PowerShell.

Opening an URL as a new tab

If we want to open an additional URL in a new tab, we can pass some Flags to the Navigate function.

The 2 main flags are:

$navOpenInNewTab = 0x0800; 
$navOpenInBackgroundTab = 0x1000;

To open an additional tab, all you need to do is:

#Open in a new tab, and make the tab active.
$objInternetExplorer.navigate("https://www.bing.com", 0x800) 
#Open in a new tab in the background.
$objInternetExplorer.navigate("https://www.yahoo.com", 0x1000)

There are additional flags available. If you are on PowerShell v5 you can use the enum below:

enum BrowserNavConstants 
{ 
    navOpenInNewWindow = 0x1
    navNoHistory = 0x2
    navNoReadFromCache = 0x4
    navNoWriteToCache = 0x8 
    navAllowAutosearch = 0x10 
    navBrowserBar = 0x20 
    navHyperlink = 0x40 
    navEnforceRestricted = 0x80
    navNewWindowsManaged = 0x0100
    navUntrustedForDownload = 0x0200
    navTrustedForActiveX = 0x0400
    navOpenInNewTab = 0x0800
    navOpenInBackgroundTab = 0x1000
    navKeepWordWheelText = 0x2000
    navVirtualTab = 0x4000
    navBlockRedirectsXDomain = 0x8000
    navOpenNewForegroundTab = 0x10000 
}

You can open a new tab using the enum like this:

$objInternetExplorer.navigate("https://www.bing.com", [BrowserNavConstants]::navOpenInNewTab)

Opening URL’s from a list.

Time to automate this even more. This time we will use an array filled with URL’s. There is one little caveat doing this. The first page should be opened without passing a flag to the navigate method.

First of, let’s create a simple array with some URL’s.

$URLs = @(
    "www.google.be",
    "www.bing.be",
    "www.yahoo.com"
)

Next, let’s loop through each URL and open it in in Internet Explorer.

foreach($URL in $URLs)
{
    $objInternetExplorer.navigate($URL, [BrowserNavConstants]::navOpenInNewTab);
}

And this will give you this, somewhat faulty, result.

This happens because we open the first URL with the navOpenInNewTab flag as well. To fix this, we need to make a small change to our loop. Keep in mind, there are multiple ways to do this.

#Set a bool for the first tab.
$bFirstTab = $true;
foreach($URL in $URLs)
{
    #If this is our first tab;
    if($bFirstTab)
    {
        #Open it
        $objInternetExplorer.navigate($URL);
        #Set the boolean to false.
        $bFirstTab = $false;
    }
    #If it is not our first tab.
    else
    {
        #Open it with in a new tab.
        $objInternetExplorer.navigate($URL, [BrowserNavConstants]::navOpenInNewTab);
    }
}

The pages should now open nicely.

Code:

$URLs = @(
    "www.google.be",
    "www.bing.be",
    "www.yahoo.com"
)


enum BrowserNavConstants 
{ 
    navOpenInNewWindow = 0x1
    navNoHistory = 0x2
    navNoReadFromCache = 0x4
    navNoWriteToCache = 0x8 
    navAllowAutosearch = 0x10 
    navBrowserBar = 0x20 
    navHyperlink = 0x40 
    navEnforceRestricted = 0x80
    navNewWindowsManaged = 0x0100
    navUntrustedForDownload = 0x0200
    navTrustedForActiveX = 0x0400
    navOpenInNewTab = 0x0800
    navOpenInBackgroundTab = 0x1000
    navKeepWordWheelText = 0x2000
    navVirtualTab = 0x4000
    navBlockRedirectsXDomain = 0x8000
    navOpenNewForegroundTab = 0x10000 
}

$objInternetExplorer = New-Object -ComObject InternetExplorer.Application;
$objInternetExplorer.visible = $true;

#Set a bool for the first tab.
$bFirstTab = $true;
foreach($URL in $URLs)
{
    #If this is our first tab;
    if($bFirstTab)
    {
        #Open it
        $objInternetExplorer.navigate($URL);
        #Set the boolean to false.
        $bFirstTab = $false;
    }
    #If it is not our first tab.
    else
    {
        #Open it with in a new tab.
        $objInternetExplorer.navigate($URL, [BrowserNavConstants]::navOpenInNewTab);
    }
}

In part 2, we will handle SSL certificate warnings on all our tabs. Part 3 will be about automating logins.

One Reply to “PowerShell // Internet Explorer Automation – Part 1 : The Basics”

Leave a Reply

Your email address will not be published. Required fields are marked *