首页 > 系统相关 >powershell简单的使用gui

powershell简单的使用gui

时间:2023-01-07 22:56:21浏览次数:93  
标签:Windows gui Object System inputform 简单 New powershell Size

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

相关文章

  • java中String类型的相关知识的简单总结
    java中String类型的相关知识总结一、常用方法:1.构造方法:byte数组可指定offset和length可指定charsetchar数组可指定offset和count字符序列String......
  • 简单测试qt通过odbc方式连接mysql8数据库
    下载数据库mysql8.0.27,SQLyog,mysql-connector-odbc强烈推荐从镜像下载mysql-connector-odbc-8.0.27-winx64.msi​​​https://mirrors.tuna.tsinghua.edu.cn/mysql/do......
  • 简单文件的读写
    1.背景2023-01-07最近学习了文件流操作,简单记录一下2.用途百度百科:用来进行输入输出操作的流就称为IO流。换句话说,IO流就是以流的方式进行输入输出。说人话就是......
  • [ABC256E] Takahashi's Anguish 题解
    [ABC256E]Takahashi'sAnguishSolution目录[ABC256E]Takahashi'sAnguishSolution更好的阅读体验戳此进入题面SolutionCodeUPD更好的阅读体验戳此进入题面存在$n......
  • 简单算法:优先队列
    典型题目题目传送门优先队列对于蒟蒻来说,堆之类的……实在是有点不好理解。所以我们今天只从表面上讲讲什么是优先队列,并且争取做到熟练的运用(知其然不知其所以然)就好......
  • PowerShell木马免杀利器: Invoke-Obfuscation(过火绒)
    Invoke-Obfuscation简介Invoke-Obfuscation工具下载地址:https://github.com/danielbohannon/Invoke-Obfuscation这是一款针对PowerShell文件的免杀工具,此处对CS生成......
  • 35_Java中的设计模式简单了解
    Java中的设计模式注意:只是简单了解设计模式(Designpattern)​ 是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用......
  • XML简单了解
    XML一、XML基础1、XML概述:​ XML的全称为(ExtensibleMarkupLanguage),是一种可扩展的标记语言​ 标记语言:通过标签来描述数据的一门语言(标签有时我......
  • top的简单学习
    获取当前进程的全部线程jps获取jvm的进程信息.top-Hp$pid-bn1>1.txt可以获取当前特定进程的所有子进程.注意linux与Windows的不太一样.linux下面的线程实现,......
  • 简单说说:Stream.reduce()用法解析
    基本使用先举一个简单的例子:算法题:Words题目描述每个句子由多个单词组成,句子中的每个单词的长度都可能不一样,我们假设每个单词的长度Ni为该单词的重量,你需要做的就是给出整......