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

Basic HTML and HTML5: Nest an Anchor Element within a Paragraph

<h2>CatPhotoApp</h2><main><a href="http://freecatphotoapp.com" target="_blank">cat photos</a><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."><p>Kitty ipsum dolor…

PowerShell: Disable Microsoft Dynamics CRM Organization

# disableCrmOrganization.ps1 $orgname='testOrg' $domain='kimconnect.com' $dnsServer='10.10.10.10' $credentials=@{ 'kimconnect\crmAdmin'='password'; 'kimconnect\devAdmin'='password'; } $usernamePrefix='kimconnect\crm' $servernameSuffix='-app01' $ipDictionary=@{ 'dev01'='1.1.1.1'; 'prod01'='2.2.2.2'; }…

PowerShell: Get Windows Resource Utilization

# getWindowsResourceUtilization.ps1 # version 0.02 # Gather information on a list of Windows Machines #…