Power Shell updating property - powershell

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

Related

Powershell select item in dropdown list using internetexplorer.application

I am attempting to select a dropdown item via Powershell. Its using Javascript. So far I'm only achieved to login and get a bunch of methods by getting the Element. See below.
# Create an ie com object
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
# Wait for the page to load
while ($ie.Busy -eq $true){ Start-Sleep -Milliseconds 1000; }
# Login
Write-Host -ForegroundColor Green "Attempting to login.";
# Add login details
try
{
$ie.Document.IHTMLDocument3_getElementsByName("user") = $username
$ie.Document.IHTMLDocument3_getElementsByName("pass") = $password
$ie.Document.IHTMLDocument3_getElementsByName("submit")
Do{Start-Sleep -Milliseconds 100}While($ie.Busy -eq $True)
}
catch
{
$_.Exception.Message
}
#get dropdown elementarray
$ie.Document.IHTMLDocument3_getElementById('contentPlaceHolderId')
#get methods
$ie.Document.IHTMLDocument3_getElementById('contentPlaceHolderId') | gm
Does anybody know how to select a specific element in the placeholder? Methods are too many to fit in the post.
Thanks in advance.
For anybody else the answer is:
($ie.Document.IHTMLDocument3_getElementById('$contentPlaceHolderId') | Where-Object { $_.innerHTML -eq '$DropDownElementName' }).selected = $true
Clicking a button is:
$ie.Document.IHTMLDocument3_getElementById('$button').click();

IE Automation with Powershell send ENTER Key

I am doing an IE Automation with ServiceNow where there is an option to fill the search data but there is no search button available to use the CLICK method. So I am looking for the method to enter key like {ENTER} or {~} once I filled the search data. But I am in a middle stage of PowerShell scripting and not sure how to use that.
If someone could help me with the method that would be greatly appreciate.
$IE = New-Object -ComObject InternetExplorer.application
$IE.FullScreen = $false
$IE.Visible = $true
$IE.Navigate($ServiceNowURL)
While ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 50
}
$Enter = Read-Host 'To continue press ENTER'
#Enter
$Search = $IE.Document.IHTMLDocument3_getElementsByTagName('input') | ? {$_.id -eq 'sysparm_search'}
$EnterValue = $Search.value() = $TicketNumber
First, you need to active IE window and bring it to front using AppActivate, then set focus to the search area using focus(). After that, you can send Enter key using SendKeys.
I use https://www.google.com to search as an example and you can refer to my code sample below. I test it and it works well:
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = New-Object -ComObject 'InternetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.google.com") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$search=$ie.Document.getElementsByName("q")[0] #change it to your own selector
$search.value="PowerShell" #change it to your own search value
Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
$search.focus()
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");

powershell click on internet explorer popup

I need to use PowerShell to hit close on this pop up window which appears when I open internet explorer. Hitting enter key also closes the pop up.
What I've tried
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = new-object -com internetexplorer.application
$ie.visible = $true
$ie.navigate('http://website/')
while ($ie.busy) { Start-Sleep 3 }
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
Start-Sleep 3
$link = $ie.Document.getElementsByTagName('Button') | where-object { $_.innerText -eq 'Simple Setup' }
$link.click()
Start-Sleep 2
$ie.quit()
Continuing from my comment.
Others have run into this dialog and others, and, as stated, used Selenium, AutoIT, et., to deal with that; while others have tried different means.
For Example:
# using the process handle of that dialog
$ws = New-Object -ComObject WScript.Shell
$ld = (gps iex* | where {$_.MainWindowTitle }).id
if($ld.Count -gt 1)
{
$ws.AppActivate($ld[1])
$ws.sendkeys("{ENTER}")
}
# Using the WASP module
# (note - though the code for this module is still available, the DLL is not. So, you have to compile that yourself.)
Import-Module WASP
while ($true) {
[System.Threading.Thread]::Sleep(200)
$confirmation = Select-Window iexplore
if ($confirmation -ne $null)
{
Select-ChildWindow -Window $confirmation |
Select-Control -title "OK" -recurse |
Send-Click
}
}
btw..
"Hitting enter key also closes the pop up"
... that is because modal dialogs always take focus until they are dismissed.

Powershell Internet Explorer Automation

Trying to get powershell to start different websites at some time intervals.
Here is a script that works:
function IEWeb {
$ie = New-Object -Comobject 'InternetExplorer.Application'
$ie.visible=$true
Do
{
$ie.navigate('http://p-captas02.int.addom.dk/cap-tas-views/Queue.aspx')
start-sleep 15
$ie.navigate('https://oneview.int.addom.dk/dashboard?dashboard_id=1')
start-sleep 15
$ie.navigate('https://oneview.int.addom.dk/dashboard?time=0&scroll_value=15&dashboard_id=10')
start-sleep 15
}
While ($ie.name -contains 'Internet Explorer')
}#Function
The problem is that it does not work every time
Is there anyone who knows another way of doing it?
It is important that the websites are started in the same tab
I think it would be better to check if IE has not been closed by the user at some point before trying to navigate to the next url. Also, $ie.name is a String, so $ie.name -contains 'Internet Explorer' would be wrong.
Maybe this works better for you.
function IEWeb {
# create an array with the urls you want to revolve
$urls = 'http://p-captas02.int.addom.dk/cap-tas-views/Queue.aspx',
'https://oneview.int.addom.dk/dashboard?dashboard_id=1',
'https://oneview.int.addom.dk/dashboard?time=0&scroll_value=15&dashboard_id=10'
$ie = New-Object -Comobject 'InternetExplorer.Application'
$ie.visible=$true
$index = 0
while ($ie.HWND) { # for as long as the user does not close IE
$ie.navigate($urls[$index])
Start-Sleep 15
# increment the array counter, and revert to index 0 if $urls length is reached
$index = ($index + 1) % $urls.Count
}
try {
# close and release the Com object from memory
$ie.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
catch {}
}
IEWeb

Getting existing Internet Explorer Window Created by PowerShell

I need to interact with a IE window that was previously created in PowerShell without using a global variable.
If(WindowAlreadyCreatedByPowerShell){
$IE = WindowAlreadyCreatedByPowerShell
}Else{
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($url);
}
Note: Using ((New-Object -ComObject Shell.Application).Windows).Invoke() | ?{$_.Name -eq "Internet Explorer"} returns nothing
So you will need to store something in the parent scope - if you don't want to store the IE object, store the URL you opened, or even better, the window handle (HWND property, which should be unique). Otherwise, you won't have a reliable way of getting which window you want, especially if your session is opening other IE windows as well.
# Variable for the created IE window handle
$expectedHWND = $null
# This will enumerate all ShellWindows opened as a COM object in your session
$allShellWindows = ( New-Object -ComObject Shell.Application ).Windows()
# Get the IE object matching the HWND you stored off when you first opened IE
# If you opened other windows in the session, the HWND should be unique so you
# can get the correct window you are expecting
$existingIE = $allShellWindows | Where-Object {
$_.HWND -eq $expectedHWND
}
# Your code, slightly modified
if( $existingIE ){
# You should be able to operate on $existingIE here instead of reassigning it to $IE
Write-Output "Found existing IE session: HWND - $($existingIE.HWND), URL - $($existingIE.LocationURL)"
} else {
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($url);
$expectedHWND = $IE.HWND
}
If you run that twice, but don't set $expectedHWND to $null the second time, it will find your IE window. Note that this is a lot of work for avoiding storing a variable in the parent scope, but is technically feasible.
You will likely need to curate the sample above for your specific application, but serves as a good demonstration of getting an operable object for the IE window you previously opened.