Skip to content

Instantly share code, notes, and snippets.

@GER-NaN
Last active September 11, 2019 13:15
Show Gist options
  • Select an option

  • Save GER-NaN/eb66853133860e6b04c81e4f284d7aa2 to your computer and use it in GitHub Desktop.

Select an option

Save GER-NaN/eb66853133860e6b04c81e4f284d7aa2 to your computer and use it in GitHub Desktop.
Automation browser action with powershell
#Needs to be ran as administrator
#Performs a login and logout using a valid account
#Hardcoded local host means you need to have a Visual Studio running an instance of the app, or change the url
#hacker-line that causes this script to run in a new session as administrator if not already elevated
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs;
exit
}
#sleep so we can see events happening on screen
function Sleep-UI{
Start-Sleep -Milliseconds 600;
write-host 'sleep for UI'
}
#polls ie waiting until its ready, this should be used after navigation
function Wait-For-IE{
while ($ie.Busy -eq $true -or $ie.Document -eq $null )
{
Start-Sleep -seconds 1;
write-host 'waiting for IE'
}
}
#==============================================================================================
$username=''
$password=''
$loginButtonName = 'btn-main-login'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("http://localhost:51482/Account/Login")
Wait-For-IE
Sleep-UI
$usernamefield = $ie.Document.getElementByID('UserName')
$usernamefield.value = $username
Sleep-UI
$passwordfield = $ie.Document.getElementByID('Password')
$passwordfield.value = $password
Sleep-UI
$submitButton = $ie.document.getElementByID($loginButtonName)
$submitButton.click()
Sleep-UI
Wait-For-IE #navigation occurs in previous step
$welcomeButton = $ie.document.getElementByID('btn-header-welcome-user')
$welcomeButton.click()
Sleep-UI
$welcomeButton = $ie.document.getElementByID('link-header-dropdown-signout')
$welcomeButton.click()
#Ends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment