From this other post it looks like it is possible to get the return value of IHTMLWindow2::execScript
from Internet Explorer API, however the linked example is in C++ and I don't really understand it. I'm trying to do the same thing in PowerShell, so here's my code that shows the problem:
$ie = New-Object -COM InternetExplorer.Application
$ie.visible = $true
$ie.navigate("http://google.com")
while ( $ie.busy ) { start-sleep -m 100 }
$document = $ie.document
$window = $document.parentWindow
Function eval($jsCommand) {
# Of course it doesn't return anything here. How can I get the return value?
return $window.execScript($jsCommand, 'javascript')
}
eval 'parseInt("12")' # returns nothing
What I'm really trying to do is to make jQuery selector/objects available in my PowerShell script, so that I can do things like:
eval '$("input#selector")' | where name -eq 'username'
and more.
Update: Look at this Gist for PowerShell function to run JavaScript/JQuery and return results back to PS, with timeout. It was extended from the answer below.