orad
orad

Reputation: 16056

InternetExplorer COM document available in PowerShell ISE but not in regular PS console

The code below works in PowerShell ISE but when ran in regular PowerShell console, the document object is empty:

$ie = New-Object -COM InternetExplorer.Application
$ie.Navigate('http://www.example.com')
$ie.Visible = $true
do { Start-Sleep -m 100 } while ( $ie.busy )

$document = $ie.document
$window = $document.parentWindow

"Ready state: " + $ie.ReadyState
"Document: " + $document
"Window: " + $window

Output in PowerShell ISE:

Ready state: 4
Document: mshtml.HTMLDocumentClass
Window: System.__ComObject

Output in regular PowerShell console:

Ready state: 4
Document: mshtml.HTMLDocumentClass
Window:

When ran in PowerShell.exe, the $window and all other properties of $document are null. Why is that and how to fix it? Thanks!

Upvotes: 0

Views: 629

Answers (2)

acelent
acelent

Reputation: 8135

You should check the ReadyState property for completion, instead of the flacky Busy property, which may oscilate and serves mostly the purpose of telling if the Stop button should be available:

do { Start-Sleep -m 100 } while ( $ie.ReadyState -ne 4 )

Upvotes: 1

walid toumi
walid toumi

Reputation: 2272

Try execute your regular console with -sta option.

Upvotes: 4

Related Questions