我接触到的PowerShell GUI化工具制做有三种方式
一.Web-------------B/S架构
二.WPF------------C/S架构
三.WinForm-------C/S架构shell
一.Web
详见个人另外一篇 http://www.javashuo.com/article/p-wzatxyul-bg.html express
二.WPF
WPF介绍,能够参考 http://liutiemeng.blog.51cto.com/120361/91631/
简单说就是用来作GUI的,用的XAML,支持数据绑定。而XAML本质上就是XML,
PowerShell能够很方便的操做XML对象,这样就能实现PowerShell和WPF结合。国外有很多这方面的帖子,不过不少都比较老,再也不更新了。
https://foxdeploy.com/series/learning-gui-toolmaking-series/ 上有很详细的PowerShell+WPF的说明和介绍。最近的更新还加入了多线程。里面也有WinForm的教程。
简单总结下就是:
1.在vs studio下用XAML作好GUI界面。多线程
2.把XAML代码直接粘贴到下面PowerShell代码的@""@里,或者XAML另存为一个文件,而后在PowerShell代码里用Get-Content加载进来也能够。架构
3.下面是用PowerShell操做XAML的代码,foxdeploy已经写好的模板,直接拿来用就行。这里面没写事件控制,具体的看foxdeploy博客吧,和WinForm的同样。ide
#ERASE ALL THIS AND PUT XAML BELOW between the @" "@ $inputXML = @" <Window x:Class="Azure.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Azure" mc:Ignorable="d" Title="iVision Azure Accelerator" Height="524.256" Width="332.076"> <Grid Margin="0,0,174,0"> </Grid> </Window> "@ $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = $inputXML #Read XAML $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)" throw} #=========================================================================== # Load XAML Objects In PowerShell #=========================================================================== $xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)"; try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop} catch{throw} } Function Get-FormVariables{ if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true} write-host "Found the following interactable elements from our form" -ForegroundColor Cyan get-variable WPF* } Get-FormVariables #=========================================================================== # Actually make the objects work #=========================================================================== #Sample entry of how to add data to a field #$vmpicklistView.items.Add([pscustomobject]@{'VMName'=($_).Name;Status=$_.Status;Other="Yes"}) #=========================================================================== # Shows the form #=========================================================================== write-host "To show the form, run the following" -ForegroundColor Cyan '$Form.ShowDialog() | out-null' $Form.ShowDialog() | out-null
4.运行powershell脚本文件就能够了
上个图,这个是参考彪锅的WinForm版的样子作的函数
5.记得VS studio的这个插件PowerShell Tools Pro,能够直接在VS studio中进行PowerShell+WPF的开发,原理和上面都是同样的,只不过是封装了不少的函数。工具
三.WinForm
WinForm是微软旧的GUI技术。和PowerShell结合方面与WPF很相似,只不过GUI不是用XAML来写。最简单的方法就是用Powershell Studio这个工具来作,看名字就知道了,专门用于PowerShell图形化工具制做用的。ui