Skip to content

Instantly share code, notes, and snippets.

Created August 31, 2016 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/133a105f31a25d57e6d3eb5c382a2111 to your computer and use it in GitHub Desktop.
Save anonymous/133a105f31a25d57e6d3eb5c382a2111 to your computer and use it in GitHub Desktop.
Getting InvokeMethodOnNull when the object exists
$texPasswordFile = "C:\users\$env:username\website.txt"
$tsPasswordFile = "C:\users\$env:username\website2.txt"
$texLoginFields = @("txtUser", "txtPassword", "Login", "username")
$tsLoginFields = @("test", "test")
$telestaff = "https://www.telestaff.net/servlet/ServletController?device=stdbrowser&action=doBeginLogin"
$texcom = "https://www.texcom.com/login.php"
# Function to return a password from an encrypted file, or store it if file not found
Function get-password($credentialsfile)
{
#Check to see if the file exists
if (-not (Test-Path $credentialsfile))
{
#If not, then prompt user for the credential
$creds = Get-Credential
#Get the password part
$encpassword = $creds.password
# Convert it from secure string and save it to the specified file
$encpassword |ConvertFrom-SecureString |Set-Content $credentialsfile
Remove-Item $creds
}
else
{
#If the file exists, get the content and convert it back to secure string
$encpassword = get-content $credentialsfile | convertto-securestring
}
# Use the Marshal classes to create a pointer to the secure string in memory
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($encpassword)
# Change the value at the pointer back to unicode (i.e. plaintext)
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
# Clear the pointer and the secure string
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($ptr)
$encpassword.Dispose()
remove-variable encpassword,ptr
# Return the decrypted password
return $pass
}
#Function to launch IE and login to a site: returns the IE object so it can be interacted with later
Function Start-IE($startSite)
{
$ie = New-Object -com InternetExplorer.Application
$ie.Navigate($startSite)
$ie.Visible = $true
#wait for page to load
while ($ie.Busy)
{
start-sleep -s 1
}
login-website $startSite $ie
Return $ie
}
#Function to log in to site (use "show source" to locate element IDs and customize for each site)
#If additional sites are desired will likely need to add separate cases for each site
#credit to https://www.sepago.com/blog/2016/05/03/powershell-exception-0x800a01b6-while-using-getelementsbytagname-getelementsbyname
Function login-website($loginSite, $ieobj)
{
switch ($loginSite)
{
$texcom
{
$user = $ieobj.document.IHTMLDocument3_getElementByID($texLoginFields[0])
$user.value = $texLoginFields[3]
$pwd = $ieobj.document.IHTMLDocument3_getElementByID($texLoginFields[1])
$pwd.value = (get-password $texPasswordFile)
$login = $ieobj.document.IHTMLDocument3_getElementByID($texLoginFields[2])
$login.click()
}
$telestaff
{
$fields = $ieobj.document.IHTMLDocument3_getElementsByTagName("INPUT")
$fields[2].value = $tsLoginFields[0]
$fields[3].value = (get-password $tsPasswordFile)
$fields[4].value = $tsLoginFields[1]
$fields[5].click()
#write-host $fields
}
default
{
Write-Debug("no supported website submitted to function")
}
}
}
#Function to test for login page and redo login if necessary
#note: if not on login page, the active page will be reloaded to hopefully maintain session activity
function test-logonPage($ieObjToTest)
{
switch ($ieObjToTest.LocationURL)
{
<#$texcom
{
login-website $texcom, $ieObjToTest
}
$telestaff
{
login-website $telestaff, $ieObjToTest
}#>
default
{
$ieObjToTest.Refresh()
}
}
}
#Launch websites
$ieTex = Start-IE $texcom
$ieTS = Start-IE $telestaff
#reload page after 1 minute
Start-Sleep -s 60
test-logonPage $ieTex
test-logonPage $ieTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment