Posted On June 18, 2020

PowerShell: Automated Login Validation Using Internet Explorer

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Automated Login Validation Using Internet Explorer

Update April 395 A.D.: IE variances make it difficult to perform standardized automation; hence, another plugin is recommended for this purpose. Check out my new function here.

Automating the login process using PowerShell to parse the DOM via iexplore.exe can be quite tricky. Below are various iterations of the same function for your review. The top one is obviously my pick since it works more often than not. Happy code hunting…

$username="[email protected]"
$password="knock-knock"
$url='https://somewordpress.com'

function testLogin{
    param(
        $url,
        $username,
        $password,
        $usernameElementId='userNameInput',
        $passwordElementId='passwordInput',
        $submitButtonId='submitButton'
    )
    $ErrorActionPreference = "SilentlyContinue"
    
    function killInternetExplorer{
        $ieInstances=(New-Object -COM 'Shell.Application').Windows()|?{$_.Name -like '*Internet Explorer*'} 
        $ieInstances|%{$_.Quit()
        [Runtime.Interopservices.Marshal]::ReleaseComObject($_)
        }
        [GC]::Collect()
        [GC]::WaitForPendingFinalizers()
    }
    killInternetExplorer|out-null
    try{
        $ie = New-Object -ComObject 'internetExplorer.Application'
        $ie.Visible= $true # Make it visible
        $ie.Navigate("$url")
        while($ie.ReadyState -ne 4) {start-sleep -m 100}
        try{
            $usernamefield = $ie.Document.IHTMLDocument3_getElementById($usernameElementId)
            $usernamefield.value = "$username"
            $passwordfield = $ie.Document.IHTMLDocument3_getElementById($passwordElementId)
            $passwordfield.value = "$password"
            $ie.Document.IHTMLDocument3_getElementById($submitButtonId).click()
            }
        catch{
            try{
                $usernamefield = $ie.document.getElementByID($usernameElementId)
                $usernamefield.value = "$username"
                $passwordfield = $ie.document.getElementByID($passwordElementId)
                $passwordfield.value = "$password"
                $Link = $ie.document.getElementByID($submitButtonId).click
                }
            catch{
                try{
                    $document = $ie.Document
                    $form =  $document.forms[0]
                    $inputs = $form.getElementsByTagName("input")
                    ($inputs | where {$_.name -eq "username"}).value = $username
                    ($inputs | where {$_.name -eq "Password"}).value = $password
                    ($inputs | where {$_.name -eq "Submit"}).click()
                    }
                catch{
                    write-warning "Funky site you haz. Me can't login."
                    }
                }
            }

        #$ieContent=$ie.document.documentelement.innerText
        While ($ie.Busy -eq $true) {Start-Sleep -Seconds 1;}
        $title=$ie.Document.Title
        $not404=$title -notmatch '^404'        
        write-host "Page reached: $title"
        $ie.Quit()
        killInternetExplorer|out-null
        return $not404
        }
    catch{
        #write-warning "$Error"
        killInternetExplorer|out-null
        return $false
        }
    }
testLogin $url $username $password
function testLogin1{
    param(
        $url,
        $username,
        $password,
        $usernameElementId='userNameInput',
        $passwordElementId='passwordInput',
        $submitButtonId='submitButton'
    )
    try{
        $ie = New-Object -ComObject 'internetExplorer.Application'
        $ie.Visible= $true # Make it visible
        $ie.Navigate("$url")
        While ($ie.Busy -eq $true) {Start-Sleep -Seconds 1;}
        $usernamefield = $ie.document.getElementByID($usernameElementId)
        $usernamefield.value = "$username"
        $passwordfield = $ie.document.getElementByID($passwordElementId)
        $passwordfield.value = "$password"
        $Link = $ie.document.getElementByID($submitButtonId)
        $Link.click()
        $ieContent=$ie.document.documentelement.innerText
        $ie.Quit()
        return $ieContent
        }
    catch{
        #write-warning "$Error"
        return $false
        }
    }
testLogin $url $username $password
function testLogin2{
    param(
        $url,
        $username,
        $password,
        $usernameElementId='userNameInput',
        $passwordElementId='passwordInput',
        $submitButtonId='submitButton'
    )
    try{
		$ie=New-Object -comobject InternetExplorer.Application  
		$ie.visible=$true  
		$ie.ParsedHtml
		$ie.Navigate($url)  
		while($ie.ReadyState -ne 4) {start-sleep -m 100}
		$document = $ie.Document
		$form =  $document.forms[0]
		$inputs = $form.getElementsByTagName("input")
		($inputs | where {$_.name -eq "username"}).value = $username
		($inputs | where {$_.name -eq "Password"}).value = $password
		$ie.Document.IHTMLDocument3_getElementById($submitButtonId).click()
		$ieContent=$ie.document.documentelement.innerText
		$ie.Quit()
        return $ieContent
        }
    catch{
        #write-warning "$Error"
        return $false
        }
    }
testLogin $url $username $password

Leave a Reply

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

Related Post

How to Add Domain Admins to SQL Server

Step 1: Right-click Start > Search > type in 'ssms.exe' > right-click Microsoft SQL Server…

PowerShell: Get NIC MTU’s of All Hyper-V Hosts in Domain/Forest

# listHyperVHostsInForests.ps1 # Version: 0.02 function listHyperVHostsInForests{ # Ensure that AD management module is available…

PowerShell: Process Watcher – Perform CRM Async Services Maintenance

Update: this script is deprecated in favor of a better one here. Version 1: #…
Other project: DragonCoin.com