powershell作为win的官方shell,可以调用.net对象。让它可以无所不能。例如给脚本加上图形界面更直观。而且这是win占用文件体积最小的图形程序。几行文本就能构建一个gui程序。py虽然也可以,但是要装环境。
参考网址 PowerShell GUI |使用示例构建 GUI 所涉及的步骤 (educba.com)
如何为 PowerShell 脚本创建 GUI?– IT宝 (theitbros.com)
从简单到复杂
1.使用Out-GridView来展示结果。
1.1以下脚本将列出 Windows 上运行的服务
$Svcs = Get-Service | Where-Object {$_.Status -EQ "Running"}| Out-GridView -Title "List of running services" -PassThru| Select -ExpandProperty Name
1.2构建一个 GUI,以使用 Out-Gridview cmdlet 将当前正在运行的进程导出到具有筛选器的窗体。可以看出,可以根据进程 CPU、进程 SI、进程名称或进程 ID 对结果进行排序。还有一个用于添加过滤条件的过滤器。还有一个用于搜索输出的搜索框。这为用户提供了更好的GUI,用户可以直接在屏幕上过滤或执行任何他想做的事情,而不是将输出发送到文件,然后手动搜索或过滤。其中只有一个 cmdlet,因此未创建任何函数。为四列创建一个简单的数组,并分配其相应的值。
Write-Host "Demo of dispalying out grid in a GUI with search" -ForegroundColor Green $pro_arry = @() $processes = Get-Process | Select Name, Id, CPU, SI Foreach ($process in $processes){ $pro_arry += New-Object PSObject -Property @{ Process_Name = $process.Name Process_CPU = $process.CPU Process_Id = $process.Id Process_SI = $process.SI } } $op = $pro_arry | Out-GridView -Title "Custome grid with Filters" -OutputMode Multiple
2.一些常用的交互
2.1显示消息框
[void] [System.Windows.MessageBox]::Show( "All changes have been implemented successfully ", "Script completed", "OK", "Information" )
2.2 显示一个消息框,其中包含必选项:
$answer = [System.Windows.MessageBox]::Show( "Dou you want to remove this user?", " Removal Confirmation", "YesNoCancel", "Warning" )
2.3 显示默认的 Windows 选择文件对话框,并按文件名进行筛选
Add-Type -AssemblyName System.Windows.Forms $initialDirectory = [Environment]::GetFolderPath('Desktop') $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.InitialDirectory = $initialDirectory $OpenFileDialog.Filter = 'Script files (*.ps1;*.cmd;*.bat)|*.ps1;*.bat;*.cmd' $OpenFileDialog.Multiselect = $false $response = $OpenFileDialog.ShowDialog( ) # $response can return OK or Cancel if ( $response -eq 'OK' ) { Write-Host 'You selected the file:' $OpenFileDialog.FileName }
2.4打开浏览文件夹对话框窗口
$shell = New-Object -ComObject Shell.Application $selectedfolder = $shell.BrowseForFolder( 0, 'Select a folder to proceed', 16, $shell.NameSpace( 17 ).Self.Path ).Self.Path
2.5 选择打印机对话框窗体
Add-Type -AssemblyName System.Windows.Forms $prntdlg = New-Object System.Windows.Forms.PrintDialog $prntdlg.AllowCurrentPage = $false $prntdlg.AllowPrintToFile = $trur $prntdlg.AllowSelection = $false $prntdlg.AllowSomePages = $true $prntdlg.ShowNetwork = $true $response = $prntdlg.ShowDialog( )# $response can return OK or Cancel if ( $response -eq 'OK' ) { Write-Host 'Selected printer:' $prntdlg.PrinterSettings.PrinterName }
3.使用form构建。本质上是在调用.net windows.forms对象
使用过vs构建过gui程序的应该很快能看懂。例如
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $inputform = New-Object System.Windows.Forms.Form $inputform.Text = "Demo of text box input" $inputform.Size = New-Object System.Drawing.Size(300,300) $inputform.StartPosition = "CenterScreen" $OKB = New-Object System.Windows.Forms.Button $OKB.Location = New-Object System.Drawing.Size(65,120) $OKB.Size = New-Object System.Drawing.Size(65,23) $OKB.Text = "Submit" $OKB.Add_Click({Write-Host "Entered value is"$OTB.Text -ForegroundColor Green;$inputform.Close()}) $inputform.Controls.Add($OKB) $CAB = New-Object System.Windows.Forms.Button $CAB.Location = New-Object System.Drawing.Size(140,120) $CAB.Size = New-Object System.Drawing.Size(65,23) $CAB.Text = "Clear" $CAB.Add_Click({Write-Host "No input from user" -ForegroundColor Red;$inputform.Close()}) $inputform.Controls.Add($CAB) $Lbl = New-Object System.Windows.Forms.Label $Lbl.Location = New-Object System.Drawing.Size(10,20) $Lbl.Size = New-Object System.Drawing.Size(260,20) $Lbl.Text = "Please provide a value" $inputform.Controls.Add($Lbl) $OTB = New-Object System.Windows.Forms.TextBox $OTB.Location = New-Object System.Drawing.Size(10,50) $OTB.Size = New-Object System.Drawing.Size(240,20) $inputform.Controls.Add($OTB) $inputform.Topmost = $True $inputform.Add_Shown({$inputform.Activate()}) [void] $inputform.ShowDialog()
4.使用vs生成 xaml文档,ps调用构建复杂gui
请参考引用网页
5.使用专业工具。推荐powershell studio
这个是真正的专业工具。网上有你懂得版本。不过能用到这个,起码得懂不少ps和c#知识了。而且是纯英文界面
标签:Windows,gui,Object,System,inputform,简单,New,powershell,Size From: https://www.cnblogs.com/kyo413/p/17033792.html