标签:
我接触到的PowerShell GUI化工具制作有三种方法一.Web-------------B/S架构
二.WPF------------C/S架构
三.WinForm-------C/S架构
一.Web
详见我的另一篇
二.WPF
WPF介绍,可以参考
简单说就是用来做GUI的,用的XAML,撑持数据绑定。而XAML素质上就是XML,
PowerShell可以很便利的操纵XML东西,这样就能实现PowerShell和WPF结合。国外有不少这方面的帖子,不过很多都对照老,不再更新了。
https://foxdeploy.com/series/learning-gui-toolmaking-series/ 上有很详细的PowerShell+WPF的说明和介绍。比来的更新还插手了多线程。
简单总结下就是:
1.在vs studio下用XAML做好GUI界面。
2.把XAML代码直接粘贴到下面PowerShell代码的@""@里,或者XAML另存为一个文件,然后在PowerShell代码里用Get-Content加载进来也可以。
3.下面是用PowerShell操纵XAML的代码,,foxdeploy已经写好的模板,直接拿来用就行。
#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"> <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-null4.运行powershell脚本文件就可以了
上个图,这个是参考彪锅的WinForm版的样子做的
5.记得VS studio的这个插件PowerShell Tools Pro,可以直接在VS studio中进行PowerShell+WPF的开发,道理和上面都是一样的,只不过是封装了很多的函数。
三.WinForm
WinForm是微软旧的GUI技术。和PowerShell结合方面与WPF很类似,只不过GUI不是用XAML来写。最简单的要领就是用Powershell Studio这个工具来做,看名字就知道了,专门用于PowerShell图形化工具制感化的。
WPF+PowerShell制作单机版应用