Powershell and HTML with IE

Code Below works fine now.
But I need to make a change to the way IE is called. I want IE to close after it test a link and open a whole new instance of IE to do the next test. Now I pass links in a foreach loop and all the links use the same window. I need to open close IE when each link is passed to function below. How would that change the code below.

<#	#>
if (!(Get-Variable ie -Scope global -ea SilentlyContinue) -or !$ie.Document)
{
	$global:ie = New-Object -comobject InternetExplorer.Application
}
$ie.visible = $true
$ie.silent = $true

$global:ReportPart = "/interface/folderTree.jsp?rootId=216&expandLevel=1&clearUIPath=true&uiPathLabel=Reports"
#                     /interface/folderTree.jsp?rootId=216&amp;expandLevel=1&amp;clearUIPath=true&amp;uiPathLabel=My+Reports    # sample Akebono Brake Corporation	UTA 6.1

function Test-ReportSite ([String] $url, [String] $login, [String] $password)
{
	$ie.Navigate2($url)
	while ($ie.busy) { Start-Sleep -m 100 }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'loginField' } | % { $_.value = $login }
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'passwordField' } | % { $_.value = $password }
	$ie.Document.getElementsByTagName("input") | ? { $_.name -eq 'password' -and $_.type -eq "password" } | % { $_.value = $password }

	
	$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' } | % { $_.Click() }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Navigate2($url + $ReportPart)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Overtime Report' } | select -First 1 -expandProperty href
	if (!$link)
	{
		$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Count' } | select -First 1 -expandProperty href
	}
	$ie.Navigate2($link)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("button") | ? { $_.innerText -eq 'Go' } | % { $_.Click() }
	Start-Sleep 5
	
<#  Error message evaluation ignored ATM   
  $ie.Document.getElementsByTagName("span") | ft -a classname, tagname, innerText #Gets you error
#>
	
	$RunByTest = $ie.Document.body.tagname -eq 'FRAMESET' -and $ie.Document.body.id -eq 'reportViewerFrame'
	
	if ($RunByTest)
	{
		$TestValue = 200
	}
	else
	{
		$TestValue = -200
	}
	Write-Host "$URL`: $TestValue"
	return $TestValue
}

Open in new window

LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
$global:ReportPart = "/interface/folderTree.jsp?rootId=216&expandLevel=1&clearUIPath=true&uiPathLabel=Reports"
#                     /interface/folderTree.jsp?rootId=216&amp;expandLevel=1&amp;clearUIPath=true&amp;uiPathLabel=My+Reports    # sample Akebono Brake Corporation	UTA 6.1

function Test-ReportSite ([String] $url, [String] $login, [String] $password)
{
	$ie = New-Object -comobject InternetExplorer.Application
        $ie.visible = $true
        $ie.silent = $true
        $ie.Navigate2($url)
	while ($ie.busy) { Start-Sleep -m 100 }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'loginField' } | % { $_.value = $login }
	$ie.Document.getElementsByTagName("input") | ? { $_.Id -eq 'passwordField' } | % { $_.value = $password }
	$ie.Document.getElementsByTagName("input") | ? { $_.name -eq 'password' -and $_.type -eq "password" } | % { $_.value = $password }

	
	$ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button' } | % { $_.Click() }
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Navigate2($url + $ReportPart)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Overtime Report' } | select -First 1 -expandProperty href
	if (!$link)
	{
		$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Count' } | select -First 1 -expandProperty href
	}
	$ie.Navigate2($link)
	while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
	
	$ie.Document.getElementsByTagName("button") | ? { $_.innerText -eq 'Go' } | % { $_.Click() }
	Start-Sleep 5
	
<#  Error message evaluation ignored ATM   
  $ie.Document.getElementsByTagName("span") | ft -a classname, tagname, innerText #Gets you error
#>
	
	$RunByTest = $ie.Document.body.tagname -eq 'FRAMESET' -and $ie.Document.body.id -eq 'reportViewerFrame'
        $ie.Quit()
	
	if ($RunByTest)
	{
		$TestValue = 200
	}
	else
	{
		$TestValue = -200
	}
	Write-Host "$URL`: $TestValue"
	return $TestValue
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo TorresSQL DeveloperAuthor Commented:
Sorry been away from office have not been able to test. I think tomorrow I will be able to finally test.

Thanks Qlemo.
Leo TorresSQL DeveloperAuthor Commented:
Works
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
There is probably a better way than to close IE, but I cannot see any means to reset it.
Leo TorresSQL DeveloperAuthor Commented:
No Worries Thanks it works for me.

Results oriented person. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.