开发者

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 "Internetexplo开发者_C百科rer.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


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()


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


I know you specified an HTML form but you might be better off writing a WinForms GUI for this instead.

http://www.techotopia.com/index.php/Creating_GUIs_in_Windows_PowerShell_1.0_with_WinForms


Because you are already able to open a new ie window from your powershell script and navigate to the correct page, I'm assuming that you are using InternetExplorer Object. That's why I'm thinking @ravikanth link is good for you. From there (and from many other posts on the internet) you can see how to grab value fields using $ie.Document.getElementById("fieldid").value, do you?

Another option is System.Net.WebClient which is the proper way to GET/POST web page data. You have a nice example here.

Otherwise, I think you should be more clear about what you want to do and provide some code sample of what you are trying to do.


I've been wondering if this actually works well:

http://social.technet.microsoft.com/wiki/contents/articles/how-to-add-a-graphical-user-interface-to-a-powershell-script-by-using-html-applications.aspx

It's based around running a PowerShell process from VBScript and then using the clipboard to transfer the output back to the HTA.

Sounds like a half decent hack, I've not tried it for myself yet though.

Matt


I add javascript by using the execScript("SCRIPT HERE","Type Of Script") of the parent window

$html = @"
<html>
    <body>
        <form>
                First name: <input type="text" id="firstname">
            <br>
                Last name: <input type="text" id="lastname">
            <br>
            <input type="button" id="finished" value="Submit" onclick=''>
            </form>
    </body>
</html>
"@

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("about:blank")
$ie.document.body.innerHTML = $html
$doc = $ie.Document
$doc.parentWindow.execScript("document.getElementById('finished').onclick = function(){document.getElementById('finished').value = 'Submitted';};","javascript")
$ie.Visible = $true

while ($doc.getElementByID("finished").value -ne "Submitted") {
    start-sleep -m 500
    $firstName = $doc.getElementByID("firstname").value
    $lastName = $doc.getElementByID("lastname").value
}

$ie.Visible = $false
"You answered:"
$doc.getElementByID("firstname").value
$doc.getElementByID("lastname").value
$ie.quit()


@Clayton It works fine for on other windows 10 OS System but gives an error on Windows 10 Azure VM

Error- The property 'innerHTML' cannot be found on this object. Verify that the property exists and can be set. At line:24 char:1

  • $ie.document.body.innerHTML = $html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜