Author Posts

March 15, 2016 at 11:24 am

I was able to do this with Powershell v3 however I discovered that the large majority of computers in the environment I work in are running Powershell v2 which puts a huge kink in what I am trying to accomplish.

Goal: To remotely execute a powershell command that will return (not the code) but the resulting web content generated by executing the code in the default browser of that remote computer.

Powershell v3 code:
$webresponse = Invoke-WebRequest -Uri $url
$response = $webresponse.allelements[0].innertext

I was able to successfully convert this into Powershell code that works on v2.

Powershell v2:
$ParsedHTML = New-Object -com "HTMLFILE"
$webresponse = (New-Object System.Net.WebClient).DownloadString($url)
$ParsedHTML.IHTMLDocument2_write($webresponse)
$response = $ParsedHTML.Body.InnerText

The main problem is that when you run this code on any computer;
$ParsedHTML.IHTMLDocument2_write($webresponse)
it prompts the user with a yes or no response to the following popup.

"Windows Security Warning
To allow this website to provide information personalized for you, will you allow it to put a small file (called a cookie) on your computer?"

I am attempting to find a method to auto accept this prompt, to allow my script to work but more over the website that I am going to is checking to see if cookies are enabled and by clicking yes or no it changes that resulting output.

Note: If you are interested in trying it out yourself
$url = "http://www.respondus.com/browser/ie.pl"

March 15, 2016 at 11:54 am

It's a bit tricky, as those COM objects are just using Internet Explorer (and not in a way that is really safe for scripting, as you've found; they will pop up GUI elements from time to time.)

If you don't mind distributing a DLL in order to make your script work, I'm a big fan of the HTML Agility Pack for this sort of thing: https://htmlagilitypack.codeplex.com/ . Check out http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/ for a nice blog / example of using this from PowerShell.

March 15, 2016 at 12:42 pm

Thank you for your reply Dave. I am looking to avoid any 3rd party tools if at all possible. Would .net tools like cookiecontainers be a better option? Or do you know if the invoke-webrequest code is posted somewhere?