使用 HTML 表单作为 powershell 的 GUI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5981905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use HTML form as GUI for powershell
提问by Steve
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?
我有一个我想使用 html 表单运行的 powershell 脚本。我只有几个表单域和一个按钮。当我运行我的 powershell 脚本时,它会打开一个新的 ie 窗口,然后导航到带有表单的正确页面。在用户单击按钮后,如何收集在表单中填写的信息?
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
无法注册活动。不支持需要返回值的事件
回答by Clayton
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.
我试图完成同样的事情,但我看到了你的帖子。我看到已经有大约 8 个月了,但如果你还在看,这就是我发现的。我无法让“onclick”工作。所以我使用了一个隐藏的输入字段,并为按钮定义了一个操作来更新该字段的值。在 powershell 中,我为隐藏字段获取了 ElementbyId,并使用它来识别用户已完成输入。
$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()
回答by ravikanth
The following article on Web UI automation with PowerShell may help you. http://msdn.microsoft.com/en-us/magazine/cc337896.aspx
以下有关使用 PowerShell 进行 Web UI 自动化的文章可能会对您有所帮助。http://msdn.microsoft.com/en-us/magazine/cc337896.aspx
回答by Scott Keck-Warren
I know you specified an HTML form but you might be better off writing a WinForms GUI for this instead.
我知道您指定了一个 HTML 表单,但您最好为此编写一个 WinForms GUI。
http://www.techotopia.com/index.php/Creating_GUIs_in_Windows_PowerShell_1.0_with_WinForms
http://www.techotopia.com/index.php/Creating_GUIs_in_Windows_PowerShell_1.0_with_WinForms
回答by ArcSet
I add javascript by using the execScript("SCRIPT HERE","Type Of Script") of the parent window
我通过使用父窗口的 execScript("SCRIPT HERE","Type Of Script") 添加 javascript
$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()
回答by Emiliano Poggi
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?
因为您已经能够从您的 powershell 脚本打开一个新的 ie 窗口并导航到正确的页面,所以我假设您正在使用InternetExplorer Object。这就是为什么我认为 @ravikanth 链接对你有好处。从那里(以及互联网上的许多其他帖子)您可以看到如何使用 获取值字段$ie.Document.getElementById("fieldid").value
,是吗?
Another option is System.Net.WebClientwhich is the proper way to GET/POST web page data. You have a nice example here.
另一个选项是System.Net.WebClient,这是获取/发布网页数据的正确方法。你在这里有一个很好的例子。
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.
否则,我认为您应该更清楚自己想要做什么,并提供一些您正在尝试做的代码示例。
回答by Matt
I've been wondering if this actually works well:
我一直想知道这是否真的有效:
It's based around running a PowerShell process from VBScript and then using the clipboard to transfer the output back to the HTA.
它基于从 VBScript 运行 PowerShell 进程,然后使用剪贴板将输出传输回 HTA。
Sounds like a half decent hack, I've not tried it for myself yet though.
听起来像是一个不错的黑客,但我还没有为自己尝试过。
Matt
马特