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

HAProxy Example for SSH & OpenVNP forwarding

# Source: https://limbenjamin.com/articles/running-https-ssh-vpn-on-port-443.html   global tune.ssl.default-dh-param 2048   defaults timeout connect 5000 timeout client 50000 timeout…

Sendmail Batch File

1. Download: 2. Place it in C:\Windows\System32   sendmail.bat --------------------------- senditquiet.exe -s smtp.gmail.com -port 587…

PowerShell: Setting Windows Firewall Rules

# Set variables for HTTP$protocolName="HTTP"$protocol="TCP"$portNumbers='80','443'$direction="Inbound"$scopes='Domain', 'Private'# Add Firewall RuleNew-NetFirewallRule -DisplayName "$protocolName-$direction" -Profile @($scopes) -Direction $direction…
Other project: DragonCoin.com