0

I'd like to save a webpage (ASP) exactly as IE would save it as a text file.

I found this and tried:

$ie=New-Object -ComObject InternetExplorer.Application
$ie.Navigate($Page)
$ie.Document.execCommand("SaveAs", $false, "1.txt")

But I'm getting the error:

You cannot call a method on a null-valued expression.
At line:3 char:1
+ $ie.Document.execCommand("SaveAs", $false, "1.txt")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I'm not sure what I'm doing wrong.

If I try to capture the page via

$web = New-Object Net.WebClient
$web | Get-Member
$web.DownloadString($Page) | Out-File "test.html"

I get a page, but it's an error page created by the site letting me know the link is invalid, so it only actually finds the page if I load it through an IE session.

I really just need to automate capturing the IE page as a text file, and I'm hoping to do it without using the SendKey function. Can anyone assist?

Nate
  • 802
  • 1
  • 8
  • 28
  • Possibly you're not waiting for IE to finish loading the page and parse the HTML and generate a DOM, so there is no .Document yet. You typically want to `.Navigate(..)` and then `while ($ie.Busy -or $ie.ReadyState -ne 4) { start-sleep -Milliseconds 100 }` to wait for it to load the content, before doing anything with it. – TessellatingHeckler Apr 19 '17 at 00:59
  • Thanks. Maybe that's it, but right now it's running/hanging. Shouldn't it only be 1/10th a second delay with that? – Nate Apr 19 '17 at 01:06
  • @TessellatingHeckler I also tried a `Start-Sleep 10` between navigate and execCommand, but got the same error. Adding the line you suggest still just hangs forever. Ideas? – Nate Apr 19 '17 at 01:15
  • Right now it's running? What happened to the `You cannot call a method on a null-valued expression.` ? It won't be a 1/10th of a second delay, it will be an endless delay because it's in a loop until IE reports it's finished. – TessellatingHeckler Apr 19 '17 at 01:16
  • idea: you're navigating to a "trusted site", IE is reloading it in a new protected mode process and your `$ie` com object no longer points to anything? – TessellatingHeckler Apr 19 '17 at 01:17
  • @TessellatingHeckler That's probably what's happening. It opens in a separate window and it's an internal page. Any idea how to work around that? Can I snag the PID and point to that or something? – Nate Apr 19 '17 at 01:29
  • You can possibly use the answer here: http://stackoverflow.com/questions/18117322/powershell-web-page-automation-works-on-internet-not-intranet – TessellatingHeckler Apr 19 '17 at 01:42
  • Thanks. I'll check into that tomorrow. – Nate Apr 19 '17 at 01:44
  • Why don't you check `if ($ie.busy)` and then put the sleep accordingly – Ranadip Dutta Apr 19 '17 at 03:30

1 Answers1

0

Try Invoke-WebRequest:

Invoke-WebRequest -Uri "http://www.powershell.org" -OutFile "powershell.txt"
hod
  • 750
  • 7
  • 21