Use HTML form as GUI for powershell

后端 未结 7 1095
北海茫月
北海茫月 2021-02-10 03:57

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

相关标签:
7条回答
  • 2021-02-10 04:05

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

    0 讨论(0)
  • 2021-02-10 04:07

    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()
    
    0 讨论(0)
  • 2021-02-10 04:09

    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()
    
    0 讨论(0)
  • 2021-02-10 04:14

    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

    0 讨论(0)
  • 2021-02-10 04:23

    @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 讨论(0)
  • 2021-02-10 04:28

    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.

    0 讨论(0)
提交回复
热议问题