Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use HTML form as GUI for powershell

I have a powershell script that I would like to run using an html form. All I have is a few form fields and a button. When I run my powershell script, it opens a new ie window, and then navigates to the correct page with the form. How do I gather the information that gets filled out in the form after the user clicks the button?

EDIT:

Here is some code I'm trying to get working:

function onClick($server)
{
    $server.value="here"
}

$ie = new-object -com "Internetexplorer.Application"
$ie.navigate("bulk_upload.html")
$ie.visible = $true

$doc = $ie.document
$btn = $doc.getElementById("submit")
$server = $doc.getElementById("server")

$btn.add_onclick({onClick $server})

I run this code and nothing happens after I click the button

UPDATE

I tried running this code:

$ie = new-object -com "Internetexplorer.Application"
$ie.navigate("bulk_upload.html")
$ie.visible = $true

$doc = $ie.document
$btn = $doc.getElementById("submit")

$eventId = Register-ObjectEvent $btn HTMLButtonElementEvents_Event_onclick -Action {write-host 'hi'}

And I get this error:

Cannot register for event. Events that require a return value are not supported

like image 829
Steve Avatar asked May 12 '11 17:05

Steve


People also ask

Can you create a GUI with PowerShell?

However, PowerShell is a powerful and modern automation tool for Windows that allows you transparently use a variety of . NET Framework objects. For example, using the . NET API, you can easily create a simple graphical interface (GUI) for your PowerShell scripts.

Does PowerShell support GUI interface?

A PowerShell graphical user interface (GUI) can help lower-XP PowerShell users to interact more safely and confidently with PowerShell. GUIs are great, because they: let users run PowerShell by themselves. provide a job-specific interface to enforce limited input.

Can PowerShell read an HTML file?

Get-Content cmdlet is used to read content of a html file.


2 Answers

I was trying to accomplish the same thing and ran across your post. I see it's been about 8 months, but if you're still looking here's what I found. I was not able to get "onclick" to work. So I used a hidden input field, and defined an action for the button to update the value of that field. In powershell I getElementbyId for the hidden field, and use that to recognize the user has completed the input.

$html = @"
<html>
<body>
<form>
First name: <input type="text" id="firstname">
<br>
Last name: <input type="text" id="lastname">
<br>
<input type="hidden" name="finished" value="0">
<input type="button" id="button" value="Continue" onclick="finished.value=1">
</form>
</body>
</html>
"@

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("about:blank")
$ie.AddressBar  = $false
$ie.MenuBar     = $false
$ie.StatusBar   = $false

$ie.document.body.innerHTML = $html
$ie.Visible = $true
$doc = $ie.document

while ($doc.getElementByID("finished").value -ne 1) {
    start-sleep -m 500
}

$ie.Visible = $false
"You answered:"
$doc.getElementByID("firstname").value
$doc.getElementByID("lastname").value
$ie.quit()
like image 59
Clayton Avatar answered Oct 11 '22 11:10

Clayton


The following article on Web UI automation with PowerShell may help you. http://msdn.microsoft.com/en-us/magazine/cc337896.aspx

like image 2
ravikanth Avatar answered Oct 11 '22 11:10

ravikanth