Search code examples
powershellpostbuttoninvoke-webrequest

Invoke-Webrequest click a button and continue with a session variable


I'm trying to log into my printers to grab logs from them and parse them with powershell.

The logon box isn't a FORM, it's an unordered list containing INPUT textboxes.

So I have managed to select the PIN textbox and give it the correct value.

I have also managed to get a hold of the button I have to click, but now my problem is, I need to click that button and declare a SessionVariable so I can continue with that session once the button is clicked and I'm logged in.

How do I properly click this button? I'm trying this but it's not working:

$Page = Invoke-WebRequest -Uri "http://xxx/webglue/content?c=LoginDropdown&lang=fr"`
-Headers @{"Pragma"="no-cache"; "Accept-Encoding"="gzip, deflate"; "Accept-Language"="fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"; "User-Agent"="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"; "Accept"="application/json, text/javascript, */*; q=0.01"; "Cache-Control"="no-cache"; "X-Requested-With"="XMLHttpRequest"; "Cookie"="lang=fr; autoLogin=false"; "Referer"="http://xxx/"}`
-ContentType "application/x-www-form-urlencoded; charset=utf-8"`
-SessionVariable "SESSION"

$TextboxPIN = $page.ParsedHtml.body.getElementsByTagName('input') | Where-Object {$_.OuterHTML -match "pin"} 
$TextboxPIN.value = "xxx"

$ConnectionButton = $page.ParsedHtml.body.getElementsByTagName('button') | Where-Object {$_.onclick -like "*login(this)*"}


Invoke-WebRequest -Uri ("http://xxx/webglue/content?c=LoginDropdown&lang=fr"+ $ConnectionButton.click() ) -Method POST -Body $ConnectionButton -SessionVariable "ADMIN"

This doesn't work for now because i'm not properly logged in since I can't click the button correclty with POST

Invoke-WebRequest -uri "http://xxx/cgi-bin/history" -WebSession $ADMIN

That http:// PRINTER-IP/webglue/content?c=LoginDropdown&lang=fr"+ $BoutonConnexion.click() is based on how it's done with forms but i'm sure it's wrong in this case. How to properly click it?


Solution

  • After trying a lot of different things, none worked, here is how I ended up getting it working. It's not pretty but it works well and takes just a few seconds to extract all the logs, which I can then parse later.

    This is for a Lexmark MS621n printer but could be adapted to other Lexmark printers.

    This dumps all the log files I found of interest:

    $Printer = "IPofPrinter"
    
    $ie = New-Object -ComObject 'InternetExplorer.Application'
    $ie.visible = $true
    $ie.navigate("http://$printer")
    
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    
    $Menu = $ie.document.IHTMLDocument3_getElementById("loginclickarea")
    $menu.Click()
    start-sleep 1
    
    $pin = $ie.document.IHTMLDocument3_getelementsbytagname("input") | Where-Object {$_.OuterHTML -match "pin"} 
    $pin.focus()
    
    $wshell = New-Object -ComObject wscript.shell
    $wshell.SendKeys("XXXXX")
    
    $btn = $ie.document.IHTMLDocument3_getelementsbytagname("button") | Where-Object {$_.OuterHTML -like "*login(this)*"}
    $btn.Click()
    
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    
    
    $ie.navigate("http://$printer/cgi-bin/enginedebugdata")
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    $ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_enginedebugdata.log
    
    $ie.navigate("http://$printer/cgi-bin/eventlog_se")
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    $ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_eventlog.log
    
    
    $ie.navigate("http://$printer/cgi-bin/history")
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    $ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_history.log
    
    $ie.navigate("http://$printer/cgi-bin/se_net_details")
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    $ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_se_net_details.log
    
    $ie.navigate("http://$printer/cgi-bin/netsetuppg")
    do {start-sleep -m 100} until ($ie.ReadyState -eq 4)
    $ie.Document.documentElement.innerHTML > C:\TEMP\$($printer)_netsetuppg.html
    
    $ie.Stop()
    $ie.Quit()