URL check using Powershell fails

Topics: Developer Forum
Apr 4, 2008 at 6:30 PM
I have following Powershell scripts which runs fine as expected in the windows power shell command prompt.
But when put in Polymon for powershell monitor it fails:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://Testweb02:8088/Test.WebSetup/Pages/frmhome.aspx")
$ie.visible = $false
$doc = $ie.document
$tb1 = $doc.getElementByID("UctlHome_linkCreateTestProject")
if ($tb1 -eq $null )
{
$Status.StatusID=3
$Status.StatusText=" Fail"
}
else
{
$Status.StatusID=1
$Status.StatusText=" Pass"
}

Error i receive when testing in polymon is:
Status: Fail
Status Message: You cannot call a method on a null-valued expression.

Basically issues is
$doc = $ie.document always returns null and hence $doc is null irrespective of any site url i give.

Please Help
Thanks in Advance
Jignesh Vyas
$doc in
Apr 4, 2008 at 7:05 PM
Rather than fussing with the Internet Explorer COM object, you may want to look into using the .Net class net.webclient.

This may not be perfect, particularly the -match against a stream, I stripped this out of a monitor I wrote that parses an XML file by feeding that stream into:
$xpdoc = System.XML.XPath.XPathDocument $WebStream
$xpn = $xpdoc.CreateNavigator()
but it should get you started.

$URI = "http://domain/path/item"
 
trap
{
	$StatusText += "Exception: " + $_
	continue
}
 
$WebStream = (new-object net.webclient).OpenRead($URI)
If ($WebStream)
{
	# Successful connection
	If (-not $WebStream -match "RegularExpression")
	{ 
		# Content failure
	}
}
else # Connection failed
{
	# Failed connection
}
Apr 4, 2008 at 8:52 PM
Thanks for the reposne i will look into it. I am fairly new to Powershell scripting so will explore it for sure.
For the problem stated by me i jst got resolution trick was to wait until ie completes navigating to url supplied.
$ie.navigate(url)
$ie.visible = $false
while($ie.busy) {start-sleep 1}
$doc = $ie.document

But yes as you mentioned rather than using COM object .Net class will be better, coz at times i rec COM failure error message for IE com object.

Thanks,
Jignesh Vyas