温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Powershell弹出窗口的几种方式

发布时间:2020-07-10 17:19:01 来源:网络 阅读:3811 作者:azrael0328 栏目:系统运维

本来这篇文章应该是上午就写完的,结果,不得不承认自己有点懒,对最近手头上做的项目做了些总结,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

Powershell弹出窗口的几种方式


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

Powershell弹出窗口的几种方式


最后一种方式就是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"

Powershell弹出窗口的几种方式


最后可以看出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

Powershell弹出窗口的几种方式







向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI