none
InternetExplorer.Application not working

    Question

  • I saw the code sample below on several blog posts including http://msdn.microsoft.com/en-us/magazine/cc163301.aspx  but it does not work for me.

    I have tried these lines of code on several sites and $ie.document is always null.
    $ie = new-object -com "InternetExplorer.Application"
    $ie.navigate("http://www.google.com")
    $ie.visible = $true
    $doc = $ie.Document

    If I try to do anything with $doc I get the error:You cannot call a method on a null-valued expression

    I am running Windows 7 Ultimate 64 bit with all updates applied.

    I got the reply below from the scripting forum:
    when you call .Navigate() method, your object is discarded automatically.

    You can easily try it out - simply type $IE on console before and after .Navigate() - can you see the difference? ;)

    I am afraid this is bug in Powershell - I remember it worked correctly in version 1, however I can confirm this behavior here (Windows 2008 with powershell v2).
    $IE object is disconnected as soon as you call .Navigate() method. I would recommend to either open bug at Connect (I could do it for you also) or report to Powershell forum.

    Any ideas?

    Wednesday, March 17, 2010 2:58 PM

Answers

  • Hi Gary,

    hope you are still around :) I just noticed that $IE is still available after navigate in your case. Can you please try to run following code?

    $IE = New-Object -ComObject "InternetExplorer.Application"
    $IE.Navigate("Google.com")
    While ($IE.Busy -eq $True) {Sleep -Milliseconds 50}
    $Doc = $IE.Document
    Write-host "Doc1: $($Doc -is [object])"
    Write-host "IE1: $($IE -is [object])"
    $IE.Visible = $True
    Write-host "IE2: $($IE -is [object])"

    I think your problem is in fact pretty easy - you are missing the "while $IE.Busy" part and $Doc gets empty value.

    If code above works, then we found it finally :)

    Martin

    Thursday, March 25, 2010 6:24 PM

All replies

  • Just FYI - I can confirm this behavior on server, however it behaves correctly on my client...

    OK - Windows 7 64b, Powershell v2, IE8
    KO - Windows 2008 32b, Powershell v2, IE7

    Martin
    Wednesday, March 17, 2010 3:19 PM
  • Wow, now I am really confused! I am using Windows 7 64b, Powershell v2, IE8 and it does not work for me at all. You are saying it works for you in that environment, just not in the server 2008?

    Gary
    Wednesday, March 17, 2010 6:13 PM
  • Have you tried Navigate2 method:

    $ie.visible = $true
    $ie.navigate2("http://www.google.com")





    Aleksandar Nikolić http://powershellers.blogspot.com http://twitter.com/alexandair
    Wednesday, March 17, 2010 6:43 PM
  • Same result for both methods...
    1.) Create $IE
    2.) Navigate() or Navigate2()

    Martin
    Wednesday, March 17, 2010 7:14 PM
  • One small hint...

    If you run following:
    $ie = new-object -com "InternetExplorer.Application"
    $ie.navigate("http://www.google.com")

    Is IE window displayed or not? It shouldn't be ($IE.Visible should be false), however in my case it appears immediately.

    Martin

    Wednesday, March 17, 2010 7:23 PM
  • And another one :)

    I just tried and it my case it works like this:

    1.) Win7, elevated -> Works
    2.) Win7, not-elevated -> Works
    3.) W2k8, elevated -> WORKS
    4.) W2k8, not-elevated -> DOESN'T WORK

    can you confirm if Powershell is elevated or not in your case?

    Martin
    Wednesday, March 17, 2010 7:30 PM
  • Well when I do not run the $ie.visible=true, I get no ie window opening as expected. I am running Win7 elevated. Is it possible I have a older version of PowerShell? I am using whatever version was installed with my Win7 Ultimate 64 bit and have auto updates turned on. Oh and navigate2 does not perform any differently. Am I the only one having this problem? Something must be different on my system from Martin's system, since it works for him.

    Gary
    Thursday, March 18, 2010 12:49 PM
  • Hi,

    no, we have the same version of Win7 and in my case it works :) However as I tested, it doesn't work as expected when I try to run it on one of my Windows 2008 servers.

    On my w2k8 server, it depends on the fact whether powershell is elevated or not.

    I just tried W2k8 R2 - WORKS and W2k8 with Powershell v1 - also WORKS.

    Martin
    Thursday, March 18, 2010 1:18 PM
  • Hi,

    This issue may also relate to "Internet Explorer Enhanced Security Configuration". We can try to disable it to test:

    Open Server Manager, choose Server Manager in the left panel, choose Configure IE ESC in the right-panel, choose Off for your user and test.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, March 23, 2010 8:44 AM
    Moderator
  • Hi Mervyn,

    Gary is using Windows 7, not server OS - I was already thinking about this :(

    Martin

    Tuesday, March 23, 2010 9:07 AM
  •  

    Martin, thank you for reminder.

    Gary, is your client in a Domain? If so, please check if any IE group policy was applied. Run "gpresult /h gp.html", you can paste the result here or send the gp.html file to tfwst@microsoft.com for research.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, March 24, 2010 8:22 AM
    Moderator
  • Hi Gary,

    hope you are still around :) I just noticed that $IE is still available after navigate in your case. Can you please try to run following code?

    $IE = New-Object -ComObject "InternetExplorer.Application"
    $IE.Navigate("Google.com")
    While ($IE.Busy -eq $True) {Sleep -Milliseconds 50}
    $Doc = $IE.Document
    Write-host "Doc1: $($Doc -is [object])"
    Write-host "IE1: $($IE -is [object])"
    $IE.Visible = $True
    Write-host "IE2: $($IE -is [object])"

    I think your problem is in fact pretty easy - you are missing the "while $IE.Busy" part and $Doc gets empty value.

    If code above works, then we found it finally :)

    Martin

    Thursday, March 25, 2010 6:24 PM