Search code examples
powershellpowershell-2.0powershell-3.0

Getting info from a page with powershell


So I want to automatize do this simple operation using the powershell:

  • Go to my website
  • Fill "USERNAME" with "myname"
  • Check "PERSONNEL_NBR" checkbox
  • Submit
  • Retrieve the PERSONNEL_NBR and store it in a variable

NOTE: ANY SOLUTION IS FINE FOR ME

I would like to use Invoke-WebRequest since it doesn't launch a proper IE session to work. This would be greatly appreciated!

This code:

$ie = $null
$ie = new-object -com internetexplorer.application
$ie.navigate('http://example.com/')
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.name -eq "username"}; $link.value = "myname"
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.value -eq "PERSONNEL_NBR"}; $link.click()
$link = $ie.Document.getElementsByTagName('input') | where-object {$_.type -eq "submit"}; $link.click()
sleep 2
$personnel_nbr = ((($ie.Document.body.outerHTML | findstr /L /C:">PERSONNEL_NBR<" ) -split "<SPAN class=tx>" | Select -Index 1).split("</SPAN>",2) | Select -Index 0)
$ie.quit()

Works, but only if I open a powershell and launch the commands from there. If I save it as a script and launch it, it doesn't work.

PS: Lowering IE security is not viable and anyway it doesn't fix the problem.

PPS: The above version is a simplified version of the code I'm using, without the out of topic stuff. The complete version is waiting for the browser, still it doesn't work (maybe due to a cookie issue? I'd be glad to solve if someone can suggest how to do it. I have already the information about the cookies, I recoredered the post with Chrome Dev Tools. I just don't know how to use that information in my script.)

PPPS: If the problem is linked to the interactive mode, can't I execute all those commands in another powershell simulating an interactive session? That would work. Anyone know how?


Second Version


This is another attempt. No errors, but it returns me the content of the page. I'm not even sure the request goes through.

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie 
$cookie.Name = "mycookiename"
$cookie.Value = "mycookievalue"
$cookie.Path = "/"
$cookie.Domain = "example.com"
$session.Cookies.Add($cookie)
$uri = "http://example.com"
$body = @{username="myname";fields="PERSONNEL_NBR"}
#THESE ARE UNSUCCESSFUL ATTEMPTS
#$r = Invoke-WebRequest -Uri $uri -WebSession $session
#$r.InputFields[34].innerHTML = "true"
#$r.InputFields[34].innerText = "true"
#$r.Forms[0].Fields["fields"] = "PERSONNEL_NBR"
#$r.Forms[0].Fields["PERSONNEL_NBR"] = "true"
#$r.InputFields[0].innerText="myname"
#$r.Forms[0].Fields["username"] = "myname"
#$r = Invoke-WebRequest -Uri $uri -WebSession $session -Method Post -Body $r
$r = Invoke-WebRequest -Uri $uri -WebSession $session -Method Post -Body $body
$r.RawContent

Solution

  • Ok, so.. The issue preventing the script from working wasn't neither the interactive mode nor the cookies. The problem was with administrator rights.

    So this code works perfectly:

    $ie = $null
    $ie = new-object -com internetexplorer.application
    $ie.navigate('http://example.com')
    $link = $ie.Document.getElementsByTagName('input') | where-object {$_.name -eq "username"}; $link.value = "myname"
    $link = $ie.Document.getElementsByTagName('input') | where-object {$_.value -eq "PERSONNEL_NBR"}; $link.click()
    $link = $ie.Document.getElementsByTagName('input') | where-object {$_.type -eq "submit"}; $link.click()
    while ($ie.Busy -eq $true -and $timer -lt 10) {
        sleep 1
        $timer++
        echo "Waiting for example.com..."
    }
    if ($timer -gt 10) {
        while ($confirmation -ne "y" -and $confirmation -ne "n") {
            $confirmation = Read-Host "`nConnection timed out. Do you want to try to grab it manually? (y/n)"
            if ($confirmation -eq 'y') {
                echo "`nPlease go to this page:`nhttp://example.com and put `"$username`" in the `"USERNAME`" field, then check the `"PERSONNEL_NBR`" checkbox and then submit."
                Read-Host "`nPERSONNEL_NBR"
            }
            elseif ($confirmation -eq 'y') {
                echo "`nRemember to add the PERSONNEL_NBR later!"
            }
            else {
                echo "`nInvalid option."
            }
        }
        $confirmation=$null
    }
    $personnel_nbr = ((($ie.Document.body.outerHTML | findstr /L /C:">PERSONNEL_NBR<" ) -split "<SPAN class=tx>" | Select -Index 1).split("</SPAN>",2) | Select -Index 0)
    echo "The PERSONNEL_NBR is $personnel_nbr"
    $ie.quit()
    

    You just need to run it as Administrator. Thanks to everyone guys, I appreciated your help and time.