標籤:learning erase oba read xaml global 分享圖片 代碼 framework
我接觸到的PowerShell GUI化工具製作有三種方式
一.Web-------------B/S架構
二.WPF------------C/S架構
三.WinForm-------C/S架構
一.Web
詳見我的另一篇 http://blog.51cto.com/640006/2059918
二.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的說明和介紹。最近的更新還加入了多線程。
簡單總結下就是:
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" 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 Cyanget-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圖形化工具製作用的。
WPF+PowerShell製作單機版應用