Powershell – How to click on link by using powershell

powershell

I'm trying to catch an onclick on our intranet. I haven't got far in my code and I don't know how I can do it and then proceed to get info from the webpage. I have this from our intranet:

innerHTML                    : <TD class=ms-gb noWrap colSpan=100><A onclick="javascript:ExpCollGroup('7111-2_', 'img_7111-2_',event, false);return false;" href="javascript:"><IMG id=img_7111
                           -2_ alt=expand src="/_layouts/images/plus.gif" border=0>&nbsp;HW Status</A> : EOL HW<SPAN style="FONT-WEIGHT: lighter; DISPLAY: inline-block">&nbsp;‎(47) </SPAN
                           ></TD>
innerText                    :  HW Status : EOL HW ‎(47) 
outerHTML                    : 
                           <TR id=group0><TD class=ms-gb noWrap colSpan=100><A onclick="javascript:ExpCollGroup('7111-2_', 'img_7111-2_',event, false);return false;" href="javascript:"><I
                           MG id=img_7111-2_ alt=expand src="/_layouts/images/plus.gif" border=0>&nbsp;HW Status</A> : EOL HW<SPAN style="FONT-WEIGHT: lighter; DISPLAY: inline-block">&nbs
                           p;‎(47) </SPAN></TD></TR>
outerText                    :  HW Status : EOL HW ‎(47) 

This is my code:

Clear-Host
$HTML = Invoke-WebRequest -uri "http://share.mycompany.org/tools/desktopmanagement/Lists/Standard%20PC%   20Hardware/HW%20Status.aspx" -UseDefaultCredentials
$Rows = ($HTML.ParsedHtml.getElementsByTagName("td") | Where{ $_.className -eq 'ms-gb' } ).innerHTML
foreach($Row in $Rows) {
    $Row
}

This is my output $Row

<A onclick="javascript:ExpCollGroup('860-1_', 'img_860-1_',event, false);return false;" href="javascript:"><IMG id=img_860-1_ alt=expand src="/_layouts/images/plus.gif" border=0>&nbsp;HW Status</A> : Approved Model<SPAN style="FONT-WEIGHT: lighter; DISPLAY: inline-block">&nbsp;‎(36) </SPAN>
<A onclick="javascript:ExpCollGroup('860-2_', 'img_860-2_',event, false);return false;" href="javascript:"><IMG id=img_860-2_ alt=expand src="/_layouts/images/plus.gif" border=0>&nbsp;HW Status</A> : EOL HW<SPAN style="FONT-WEIGHT: lighter; DISPLAY: inline-block">&nbsp;‎(47) </SPAN

I hope that is enough

Best Answer

Basically, you can't, directly, not for Javascript.

Javascript is executed by the client-side browser program: Internet Explorer, Firefox, Chrome et al. Therefore, in order to execute it, you need to make the click from within a browser.

Powershell can set up form variables and pass them to server-side processing by calling HTTP requests, but in order to parse the page and execute client-side JavaScript, you need some Javascript-aware HTML parser. Namely -- a browser.

What is generally done in such situations is akin to web scraping where a specialized browser program -- typically headless version of chrome/firefox or a more specialized browser, is ran for the needed page, and you program it with info, which mouseclicks/keyboard events to simulate.

There are separate libraries which allow integrating web scraping into general programming languages. You can perhaps utilize it and steer it from Powershell, but in any case it is another layer between PS and the webpage without which you can't do it, afaik.

Alternative would be to reprogram the relevant webpage Javscript functions (such as ExpCollGroup in your example) in Powershell, but it will likely need extra info from webpage, which might be hard to extract.