javascriptphpformspowershellweb-client

Dumbed down Powershell web client function to let me post form data easily


Ive been using an Internet Explorer automation script found here: http://www.pvle.be/2009/06/web-ui-automationtest-using-powershell/

That lets me easily post form data using commands (functions) like this:

NavigateTo "http://www.websiteURI/"
SetElementValueByName "q" "powershell variable scope"
SetElementValueByName "num" "30"
SetElementValueByName "lr" "lang_en"
ClickElementById "sb_form_go"

The above would let me post values to elements and click to submit the form.

I would like to do the equivalent with Powershell's web client using helper functions. I haven't found such a script. The closest I could find was The Scripting Guys, Send-WebRequest:

http://gallery.technet.microsoft.com/scriptcenter/7e7b6bf2-d067-48c3-96b3-b38f26a1d143

which I'm not even sure it does what I expect (since there's no working examples showing how to do what I want).

Anyway, I'd really appreciate some help to get me started to do the equivalent of what I showed up there with working examples (as simple as possible). A bonus would be to also be able to get a list of element names for a URI in order to know what form elements I want to submit.

PS: I also need to be able to specify user-agent and credentials; so, examples with these included would be ideal.


Solution

  • Have you taken a look at the Invoke-WebRequest commmand? (requires powershell 3.0 or above) I believe the following would work for submitting the data

    #POSTing data
    Invoke-WebRequest http://www.websiteURI/ `
        -UserAgent 'My User Agent' `
        -Credential $cred `
        -Method Post `
        -Body @{
            q = 'powershell variable scope'
            num = 30
            lr = 'lang_en'
        }
    

    For your bonus, the result of Invoke-WebRequest contains a collection of the InputFields on the page, which you can use to get a list of form elements to set.

    #List input elements
    Invoke-WebRequest http://www.websiteURI/ | select -ExpandProperty InputFields