查看: 282|回复: 0

[文章教程] Powershell弹出窗口的几种方式

[复制链接]
xuanxiao 发表于 2022-12-9 11:26:07 | 显示全部楼层 |阅读模式
本来这篇文章应该是上午就写完的,结果,不得不承认自己有点懒,对最近手头上做的项目做了些总结,Powershell开发运维,也做了一段时间了,今天就来说说那些简单的GUI的编程运维,首先从弹出窗口说起吧。

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

Wscript方式:

  1. function Read-MessageBoxDialog

  2. {

  3. $PopUpWin = new-object -comobject wscript.shell

  4. $PopUpWin.popup("Hello World")

  5. }

  6. Read-MessageBoxDialog
复制代码


Powershell弹出窗口的几种方式

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

  1. function Read-MessageBoxDialog

  2. {

  3. param ([string]$Message,

  4. [string]$WindowTitle,

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

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

  7. Add-Type -AssemblyName System.Windows.Forms

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

  9. }

  10. Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere" -Buttons OK -Icon Information
复制代码


Powershell弹出窗口的几种方式

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

  1. function Read-MessageBoxDialog

  2. {

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

  4. Add-Type -AssemblyName Microsoft.VisualBasic

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

  6. }

  7. Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere"
复制代码


Powershell弹出窗口的几种方式

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

  1. function Read-MessageBoxDialog

  2. {

  3. param ([string]$Message,

  4. [string]$WindowTitle,

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

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

  7. Add-Type -AssemblyName System.Windows.Forms

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

  9. }

  10. Read-MessageBoxDialog -Message "Hello World" -WindowTitle "CustomTitleHere" -Buttons OKCancel -Icon Information
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号