Use an existing instance of IE unless it contains a specified string - powershell

I am using this code to determine whether to use an existing or new instance of IE:
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}
# Elsewhere, I open an array of sites depending on what tabs are already open
foreach ($tab in (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }) {
if ($tab.LocationURL.Contains("~"))
{ $search = $true; break }
}
If a tab is open that has specified text in its title, how can I ignore that instance of IE, and use a second instance to open an array of sites in? I have tried this:
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -like ""}
if ($null -ne $ie) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}

Not sure if this is what you're looking for?
$searchString = "something"
$ie = (New-Object -COM 'Shell.Application').Windows() | where {$_.Name -eq 'Internet Explorer' -and $_.LocationName -like "*$searchString*"}
if ($null -ne $ie) {
# existing instance found
$newInstance = $false
}
else {
# create new instance
$ie = New-Object -COM 'InternetExplorer.Application'
$newInstance = $true
}

I have got it working; improvements appreciated.
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
if ((New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -notlike "**" }) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}
} else {
$ie = New-Object -COM "InternetExplorer.Application"
$newInstance = $true
}

Related

Resolve Powershell Script to Click On Google Result

i have powershell script to find a website on google result and click on website link
$IE = new-object -com internetexplorer.application
$IE.navigate("https://www.google.com/search?q=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B2%D8%A7%D8%B1%DB%8C+%D9%81%D8%A7%D8%B1%D8%B3&oq=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B0%D8%A7%D8%B1%DB%8C")
$IE.visible=$true
while ($IE.busy) {sleep 10}
$Link = ($html.Links |Where-Object { $_.class -eq 'https://khabarfarsi.com' }) |Select-Object -ExpandProperty href
$Link = ($HTML.ParsedHtml.getElementsByTagName("a") | Where {$_.className -eq 'https://khabarfarsi.com'}).InnerHTML
$Link = #($IE.Document.getElementsByTagName("a") | ? {$_.InnerHTML -like 'https://khabarfarsi.com'})[0]
if ($Link -eq $null){ $Link = $IE.Document.getElementsByTagName("a") | ? {$_.InnerHTML -like 'https://khabarfarsi.com'} }
if ($Link -eq $null){$ie.quit(); Break}
$Link.click()
I have a number of beginner trainees who often have trouble doing this. I want to create a script that they don't need to do manually.
Thank you for accompanying me in doing this
Thanks
Regards
Here is a working script that can find and click the link.
$ie = new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate("https://www.google.com/search?q=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B2%D8%A7%D8%B1%DB%8C+%D9%81%D8%A7%D8%B1%D8%B3&oq=%D8%AE%D8%A8%D8%B1%DA%AF%D8%B0%D8%A7%D8%B1%DB%8C")
while($ie.busy) {sleep 1}
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.href -eq 'https://khabarfarsi.com/w/farsnews.com'}
$link.click()
Output:

Check if login failed

I have the following code, which logs into a site that I use frequently:
param(
[string]$interval
)
function Refresh-WebPages {
"Neptun refresh every $interval seconds."
"press any key to exit."
$shell = New-Object -ComObject Shell.Application
do {
Start-Sleep -Seconds $interval
'Refreshing'
$shell.Windows() |
Where-Object { $_.Document.url } |
ForEach-Object { $_.Refresh() }
} until ( [System.Console]::KeyAvailable )
[System.Console]::ReadKey($true) | Out-Null
}
$username = "asd";
$SecureString = Read-Host "password" -AsSecureString;
$BSTR =
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString);
$PlainPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$IE=new-object -com internetexplorer.application;
$IE.navigate2("https://hallgato.neptun.elte.hu/login.aspx");
$IE.visible=$true;
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 500}
$IE.Document.getElementById('user').value = "$username";
$IE.Document.getElementById('pwd').value = "$PlainPassword";
($IE.document.getElementById('btnSubmit') |select -first 1).click();
Refresh-WebPages -interval
I want to check if the login have failed or succeeded. Can I check if the URL is different somehow?

Open an array of URLs in IE, whether IE is open or not, using PowerShell

I have created a function that opens an array of one or more URLs in IE: the first URL should be opened in the first tab; successive URLs should be opened in background tabs. If IE is already open, the first URL should be opened in a new tab to preserve existing URLs.
function OpenSites($sites) {
$isFirstSite = $true
foreach ($site in $sites) {
if ($isFirstSite) {
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie.Navigate2($site, $navOpenInNewTab)
} else { $ie.Navigate2($site) }
$isFirstSite = $false
} else { $ie.Navigate2($site, $navOpenInBackgroundTab) }
}
}
I guess the first URL should be opened only in the first tab if the IE was not started before? And all successive URLs should be opened in background tabs, right?
Update: I hope this code is what you want:
$navOpenInNewTab = 0x800;
$navOpenInBackgroundTab = 0x1000;
$ie = $null
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
sleep -milliseconds 500
$ie.visible = $true
$newInstance = $false
} else {
$ie = New-Object -COM "InternetExplorer.Application"
sleep -milliseconds 500
$ie.visible = $true
$newInstance = $true
}
#[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
#[Microsoft.VisualBasic.Interaction]::AppActivate("$($ie.LocationName) $($ie.Name)")
sleep -milliseconds 50
Write-Host $newInstance
function OpenSites($sites) {
$isFirstSite = $true
foreach ($site in $sites)
{
if ($isFirstSite -eq $true)
{
if($newInstance) {
$ie.Navigate2($site)
} else {
$ie.Navigate2($site, $navOpenInNewTab)
}
} else {
$ie.Navigate2($site, $navOpenInBackgroundTab)
}
$isFirstSite = $false
}
}
OpenSites -sites "www.bing.com","www.heise.com"

Open D drive using powershell

I am editing a powershell script (2.0 I believe) that has the following code to open the cd drive:
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.Type -eq "CD Drive" } |
foreach { $_.InvokeVerb("Eject") }
The problem is that this script now runs on a computer with multiple optical drives so they all open - I only want the D drive to open. I thought changing the script to the following would work:
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.DriveID -eq "D:" } |
foreach { $_.InvokeVerb("Eject") }
but it did not (none of the drives opened). How can I only open the D drive?
The issue you're having is that the result of $sh.Namespace(17).Items() - at least for me - doesn't include a property called DriveID. It does include "Path" though. Try the below.
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() | ? {$_.Path -eq "D:\"} | % { $_.InvokeVerb("Eject") }
Try filtering on the Path property.
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() | Where-Object { $_.Path -eq "D:\" } | ForEach-Object { $_.InvokeVerb("Eject") }
I don't see a 'DriveID' property, so the Where-Object is filtering all objects (i.e. nothing is passed to the ForEach). Try using the 'Path' property instead?
$sh = New-Object -ComObject "Shell.Application"
$sh.Namespace(17).Items() |
Where-Object { $_.Path -eq "D:\" } |
foreach { $_.InvokeVerb("Eject") }

login into a web page using powershell

I need to log on to a web page with credentials from the get-credential window. But I am getting error- Property 'Value' cannot be found on this object; make sure it exists and is settable.I have provided my powershell source code here..
$url = "ameriprisestage.service-now.com/"
$cred = Get-Credential
$username = $cred.username
$username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.ReadyState -ne 4 -or $ie.Busy)
{
start-sleep -milliseconds 100
}
#$uname=$ie.Document.getElementsByTagName("input")
#$unameBox=$uname | where { $_.name -eq "user_name" }
$unameBox=$ie.Document.getElementById("user_name")
$unameBox.value = $username
$pass=$ie.Document.getElementsByTagName("input") | ? { $_.id -eq "user_password" }
$pass.value = $password
$pass.select
$buttn=$ie.Document.getElementsByTagName("button") | ? { $_.id -eq "sysverb_login" }
$buttn.click()
while($ie.busy) {
Start-sleep 1;
}
There are multiple windows in the web page. Try to redirect it to the relevant window.
cd HKCU:\"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-itemproperty . ProxyEnable 1
$url = "https://ameriprisestage.service-now.com/"
#function call
#$cred = Get-Credential
$username = "asset_tester02"
$username = "asset_tester02"
$password = "tester02"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy -eq $true)
{
Start-Sleep 1;
}
$usr=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementbyID("user_name").value=$username
$pass=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementById("user_password").value= $password
$buttn=$ie.document.getElementbyID("gsft_main").contentWindow.document.getElementById("sysverb_login").click()