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

PowerShell Commands to Discover the Server Network, Netmask, DHCP status, and Gateway

$servername = "SERVER01"get-wmiobject win32_networkadapterconfiguration -computername $servername -ea stop | ? {$_.IPEnabled}# Result:DHCPEnabled      :…

PowerShell: Remove Nic Teaming

# Check NIC TeamingPS C:\Windows\system32> get-NetLbfoTeamName : ISCSIMembers : {iSCSI-B, iSCSI-A}TeamNics : ISCSITeamingMode : SwitchIndependentLoadBalancingAlgorithm…

Front End Web Development Menu 2019

Deployment Register a domain name: Google Domains, GodaddyManaged or Shared hosting: AWS, Hostgator, InmotionFTP/SFTP: Filezilla,…