Get URL of page in Internet Explorer using Powershell - powershell

I have a PowerShell script which will open an instance of internet explorer, go to a certain website and find a hidden link but when I go to that link it redirects me to another page.
I'd like to find the URL of the page that internet explorer is now in to be stored in a variable.
Thanks!
For those who only read code:
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$Document = $IE.navigate2("example.com")
# Do stuff
$Document = $IE.navigate2($LinkIFound)
# It redirects me...
# TODO: Find URL of page I am now in.

So if you are trying to get the current location of a document then you can use : $IE.Document.url
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$Document = $IE.navigate2("example.com")
# Do stuff
$OldUrl = $IE.Document.url
$Document = $IE.navigate2($LinkIFound)
sleep -seconds 3
$NewUrl = $IE.Document.url

Related

Auto login script

I am trying to fix a script that automatically opens a page in edge and logs in. i can get it to open the page but the script errors out without entering the login info or directing to the desired final destination. the internal server page uses j_username and j_password to id the location where the credentials are entered. Im fairly new to coding and would like help to understand what im doing wrong and what i could be doing better.
Here is the code:
$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible
start microsoft-edge:http://internal URL to company
$usernmae="user"
$password="password"
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 5;}
$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = $username
$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = $password
$Link = $ie.document.getElementByID('login-submit')
$Link.click()
{Start-Sleep -Seconds 10;}
start microsoft-edge:http://final destination internal URL to company
$ie.Quit()
Your problem was that the $ie object is not connected to the browser when you use start microsoft-edge:http://internal URL to company. Use the Navigate method:
$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("https://internal-login-page")
Do {sleep 1} While ($ie.Busy)
$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = "user"
$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = "password"
$Link = $ie.document.getElementByID('login-submit')
$Link.click()
Do {sleep 1} While ($ie.Busy)
$ie.Navigate("https://final-page")

opening webpage with powershell in IE hangs

I try to open a webpage with Powershell code in order to print the page with PDFCreator. It hangs when I try to run it; probably when opening internet explorer.
$ie = new-object -com internetexplorer.application
if ($showWindow) {
$ie.Visible = $true
} else {
$ie.Visible = $false
}
$ie.navigate($WebSiteURL)
Why does this hang and how can I fix this?

Powershell script IE Automatic Login opens another Login tab

I have a script for Internet Explorer Login which works fine. But I want to go further because after the first login, Internet Explorer opens a new tab, with another Login form. So $IE.visible = $False does not work for the second tab, which opens automatically, and also I dont know how to give focus to the second tab to input other credentials and also put it on invisible.
$variablePass = $variablePass.Text
$username = "testUser"
$username2 = "testUser2"
$password = "testPass" + $variablePass
$password2 = "testPass2"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $false
$ie.Height = 720
$ie.Width = 1280
$ie.navigate("https://mylink.com")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
#This one work fine
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("password").value = "$password"
$ie.document.getElementById("btnSubmit").click()
# Here comes the problem, after IE reach mylink.com and do the login script, the a new tab pop up with another login form
$ie.document.getElementById("username").value = "$username2"
$ie.document.getElementById("password").value = "$password2"
$ie.document.getElementById("loginBtn").click()
So, how do I do the second Login form to work. Preferably in invisible mode, I mean $IE.visible = $False
Are you able to change the configuration of IE to prevent this from happening?
https://helpdeskgeek.com/how-to/force-ie-to-open-link-in-new-tab/
You could manually configure IE, or use PowerShell to edit the registry to force that specific configuration...

Powershell IE login automation unable to find the element by ID

Can someone knowledgeable in powershell tell me why I can't find the username input field using its ID "user_name"? I have tried this on other websites and it has worked but for this one it just refuses to find it. I also tried the IHTMLDocument3_getElementsByName method and also IHTMLDocument3_getElementsByTagName looking for things like "input" or even "div". Some div classes pop up but so many are missing there and I have no idea why its missing. Please show me the light.
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($URL)
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$ie.Document.IHTMLDocument3_getElementByID($IDuserName).Value = $userName
Below is the HTML line for the Username input field
<input name="user_name" id="user_name" type="text" class="form-control" value="" autocomplete="off">
The page is built with loads of javascript. If you try opening the page without javascript enabled in your browser you won't even see the login form. this seems to be the problem for IE/PowerShell to find the form and its elements.
However you could bring the Internet Explorer window into foreground and interact with the page using Systems.Windows.Forms like this:
#ID names
$IDuserName = "user_name"
$IDuserPass = "user_password"
$url = "https://isssupport.service-now.com/"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($ie.Name)
Start-Sleep -Milliseconds 500
$ie.navigate($url)
While ($ie.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('username');
[System.Windows.Forms.SendKeys]::SendWait('{TAB}');
[System.Windows.Forms.SendKeys]::SendWait('password');
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}');
May I ask what the purpose of the script you are building is?
Best regards
Guenther

Power Shell updating property

I play around with Power Shell... And I have a newbie question to navigate with ie.
Have this Code:
# IE window
$ie = New-Object -com "InternetExplorer.Application"
$ie.visible = $true
function waitforpageload {
while ($ie.Busy -eq $true) { Start-Sleep -Milliseconds 1000; }
}
# navigate to the first page
$ie.Navigate("http://ss64.com/bash/")
waitforpageload
$ie.Document.Url # return http://ss64.com/bash/
# navigate to the second page
$ie.Navigate("http://ss64.com/ps/")
waitforpageload
$ie.Document.Url # return also http://ss64.com/bash/
and I'm wondering why $ie.Document.Url in both times return http://ss64.com/bash/
should is it possible to get http://ss64.com/ps/ in the second call?
Thanks a lot
You'll get the current location using
$ie.LocationURL
To get a list of all methods and properties available, use $ie | gm