Author Posts

December 28, 2017 at 4:28 am

I am trying to use the Invoke-WebRequest to be able to login into a website.I am not able to capture contents of homepage i.e after logging in, in Variable $R. I still get the contents of the login page in the variable $R. I have been trying to fix this for many hours and looked at many examples but nothing seems to work. Would someone be able to give me a few pointers? Thanks for the help

$R = Invoke-WebRequest https://portal.concordfax.com/ -SessionVariable FB
$Form = $R.Forms[0]
$Form.Fields["UserName"] = "XXXXX"
$Form.Fields["Password"] = "XXXXX"
$R = Invoke-WebRequest -Uri ("https://portal.concordfax.com/Account/LogOn?ReturnUrl=%2f" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields
$R.links

December 28, 2017 at 12:56 pm

Update: I managed to fix the code by updating line 5 with

$homelanding = Invoke-WebRequest ("https://portal.concordfax.com/Account/LogOn" + $R.ParsedHtml.getElementsByClassName("blueButton login").click) -WebSession $fb -Body $R -Method Post 

I am trying to click on a link on the logged in page but it doesnt seem to work i still get the contents of the $homelanding in $output1

$output1 = Invoke-WebRequest ("https://portal.concordfax.com" + $homelanding.links[8].href.click) -WebSession $fb 

I would like to add that the href is relative and not the full path. That is the reason why i am joining them and trying to invoke a Invoke-WebRequest

 PS C:\Users\XXXX> $homelanding.links[8].href
/Administration/Accounting  

December 28, 2017 at 3:50 pm

Running through a website using Invoke-WebRequest is ***HARD***. You're going to need to start by understanding exactly how that page "logs you in" – ideally using a browser like Chrome or Safari that have good developer tools, so you can see exactly what cookies are being handed back to the browser. Then, you're going to need to compare what's in your $FB variable to that – to make sure you're capturing, and handing back, the same cookie names (the values will obviously differ). You're basically reconstructing a complex part of the browser, and that isn't trivial.

"Clicking" a link this way is also hard. It would be best to (a) login, (b) capture the cookies handed back, (c) figure out what URL you want next, and (d) directly request that URL, rather than "clicking" a link. It does seem like you're trying to do that... but, if what you're meaning to "click" is actually running client-side JavaScript, you're hosed, because Invoke-WebRequest can't "run" JavaScript. It's not a browser. So you'll need ti inspect the page and see if that link is JUST a URL, or if it's also wired up to some client-side JavaScript.

Invoke-WebRequest ("https://portal.concordfax.com" + $homelanding.links[8].href.click)

That ^^ is probably wrong. You probably just want the href property, not a "click" property, to obtain the relative URL of the link. Double check what that's returning.

December 29, 2017 at 12:55 am

Since you are trying to navigate a GUI, you are going to need a 3rdP tool or 3rdP module for that sort of stuff

For example:
'wasp.codeplex.com'
'gallery.technet.microsoft.com/AutoBrowse-ec4f4384'
'autoitscript.com'

Yet, note, much of this is Windows / IE centric tools. So, if you are on another OS/browser, well, you know.