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

Virtual Machine Manager Error ID 23351 FirstBootDevice Invalid

When moving, importing, exporting Generation 2 template in VMM, the following error occurs when trying…

PowerShell: Enable Remote Desktop

$computernames=@' SERVER1 SERVER2 '@ $computers=@($computernames -split "`n" -replace "\..*$") function enableRemoteDesktop{ $regHiveTs='HKLM:\System\CurrentControlSet\Control\Terminal Server' $regKeyTs='fDenyTSConnections' $enable=0…

PowerShell: Function to Add/Remove Local Host Record

Searching through my 'magic bag of tricks,' there appears to be this little snippet that…