Powershell弹出窗口的几种方式

转载

原文地址:

https://blog.51cto.com/azrael0328/1900591

 

本来这篇文章应该是上午就写完的,结果,不得不承认自己有点懒,对最近手头上做的项目做了些总结,Powershell开发运维,也做了一段时间了,今天就来说说那些简单的GUI的编程运维,首先从弹出窗口说起吧。

弹出窗口就自己在写PS GUI代码是经常用到的有3种方式,wscript方式,Forms方式,VB方式,先说说Wscript方式,这种方式最简单,也最简陋,只需要两行代码就可以简单做出弹出窗口。

 

Wscript方式:

 

function Read-MessageBoxDialog

{

$PopUpWin = new-object -comobject wscript.shell

$PopUpWin.popup("Hello World")

}

Read-MessageBoxDialog

wKioL1iujt3QHflBAABXY3PMZ6Y970.jpg-wh_50

 

Forms方式,相对于Wscript方式来说,这种方式写的代码比较多但是呈现的样式比较亲切

 

 

function Read-MessageBoxDialog

{

param ([string]$Message,

[string]$WindowTitle,

[System.Windows.Forms.MessageBoxButtons]$Buttons = [System.Windows.Forms.MessageBoxButtons]::OK,

[System.Windows.Forms.MessageBoxIcon]$Icon = [System.Windows.Forms.MessageBoxIcon]::None)

Add-Type -AssemblyName System.Windows.Forms

return [System.Windows.Forms.MessageBox]::Show($Message, $WindowTitle, $Buttons, $Icon)

}

Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere" -Buttons OK -Icon Information

wKiom1iujw7QcVWEAABkj3GKytQ453.jpg-wh_50

 

最后一种方式就是VB方式,这种方式是在PS中调用VB的方式来进行弹出窗口,样式与Forms基本类似

function Read-MessageBoxDialog

{

param ([string]$Message,[string]$WindowTitle)

Add-Type -AssemblyName Microsoft.VisualBasic

return [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'Information',$WindowTitle)

}

Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere"

wKiom1iukbDjYGSCAABlK9to_A4128.jpg-wh_50

 

最后可以看出VB的样式与Forms样式一样,但是代码要比Forms的少很多,如果只是提示窗口,建议用VB形式,如果想让提示窗口显示为Information形式,并且OK与Cancel按钮同时存在就将Forms方式中的Buttons属性设置为 OKCancel

function Read-MessageBoxDialog

{

param ([string]$Message,

[string]$WindowTitle,

[System.Windows.Forms.MessageBoxButtons]$Buttons = [System.Windows.Forms.MessageBoxButtons]::OK,

[System.Windows.Forms.MessageBoxIcon]$Icon = [System.Windows.Forms.MessageBoxIcon]::None)

Add-Type -AssemblyName System.Windows.Forms

return [System.Windows.Forms.MessageBox]::Show($Message, $WindowTitle, $Buttons, $Icon)

}

Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere" -Buttons OKCancel -Icon Information

wKioL1iuk4Wi_DXCAABzjNhfdqo190.jpg-wh_50

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值