.net 为 Powershell 创建 GUI 前端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17841372/
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
Create a GUI front end for Powershell
提问by PnP
What is the best way to create a GUI front-end for a Powershell Script containing multiple functions - i.e. buttons in the GUI which will call said functions and spew out some output in the end.
为包含多个功能的 Powershell 脚本创建 GUI 前端的最佳方法是什么 - 即 GUI 中的按钮将调用所述函数并最终输出一些输出。
回答by mikekol
PowerShell Studio 2012from Sapien has a built-in GUI designer, but it's fairly expensive.
Sapien 的PowerShell Studio 2012有一个内置的 GUI 设计器,但它相当昂贵。
There's also the ShowUI moduleon CodePlex, which is free, as is the WPK - which is part of the PowerShellPack.
CodePlex上还有ShowUI 模块,它是免费的,WPK 也是免费的——它是PowerShellPack 的一部分。
All of these tools are great, and PowerShell is an awesome language, but if you really need to have a graphical user interface for your script, you should perhaps question whether you're actually writing a program instead. If you're actually writing a program, using a language like C# may be a better decision.
所有这些工具都很棒,而且 PowerShell 是一种很棒的语言,但是如果您真的需要为您的脚本提供图形用户界面,您或许应该质疑您是否真的在编写程序。如果您实际上是在编写程序,那么使用 C# 之类的语言可能是一个更好的决定。
If you want an example of a script with a UI, you can check out Convert-WindowsImage, which I wrote a year or so ago. It would have been much easier to write Convert-WindowsImage as a C# program, but I decided that it was more interesting to showcase what PowerShell can do.
如果您想要一个带有 UI 的脚本示例,您可以查看我大约一年前编写的Convert-WindowsImage。将 Convert-WindowsImage 编写为 C# 程序会容易得多,但我认为展示 PowerShell 的功能更有趣。
Hope that helps.
希望有帮助。
回答by JPBlanc
I steel use two solutions (well hidden on the web but still existing) to solve your problem :
我钢铁使用两种解决方案(很好地隐藏在网络上但仍然存在)来解决您的问题:
First, You can use Sapiens PrimalForms Community Edition(It's free, you just have to sign to Sapiens and go to download).This Framwork allow you to graphicaly build your DialogBox and then it generates the PowerShell Code for Windows Forms (still avalaible as shown here under).
首先,你可以使用Sapiens PrimalForms 社区版(它是免费的,你只需要登录 Sapiens 并去下载)。这个框架允许你以图形方式构建你的对话框,然后它为 Windows 窗体生成 PowerShell 代码(仍然可用,如图所示下面)。


Secondyou've got a script called PowerShell Form Converterwriten by Arnaud Petitjean (11-05-2007). Once again you have to register, the article is writen in french, but you can download a Powershell script that transform a Windows Form from a .CS file to a PS1. Like in Sapiens editor, the transformation is commented (here in french) with the place to code events.
其次,您有一个名为PowerShell Form Converter 的脚本,由 Arnaud Petitjean (11-05-2007) 编写。您必须再次注册,这篇文章是用法语写的,但您可以下载一个 Powershell 脚本,将 Windows 窗体从 .CS 文件转换为 PS1。就像在 Sapiens 编辑器中一样,转换被注释(这里是法语)与编码事件的位置。
回答by ash
You have full access to .NET libraries with PowerShell. I've used Visual Studio before to cobble together a dialog using Winforms and then copied and pasted the code it generated into my powershell script.
您可以使用 PowerShell 完全访问 .NET 库。我之前使用过 Visual Studio 使用 Winforms 拼凑一个对话框,然后将它生成的代码复制并粘贴到我的 powershell 脚本中。
something like...
就像是...
function createGUI{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
...
$main_form = New-Object System.Windows.Forms.Form
$button_OK = New-Object System.Windows.Forms.Button
....
$main_form.ShowDialog()
}
createGUI

