[玩转系统] 周五乐趣:消息框作家
作者:精品下载站 日期:2024-12-14 07:41:10 浏览:15 分类:玩电脑
周五乐趣:消息框作家
这是新功能。
#requires -version 3.0
Function New-Messagebox {
<#
.SYNOPSIS
Display a VisualBasic style message box.
.DESCRIPTION
This function will display a graphical messagebox, like the one from VisualBasic
or VBScript. You must specify a message. The default button is OKOnly and the
default icon is for Information. If you want to use the value from a button click
in a PowerShell expression, use the -Passthru parameter.
The message box will remain displayed until the user clicks a button. The box may
also not appear on top, but if you have audio enabled you should hear the Windows
exclamation sound.
As an added bonus you can use the -Voice parameter to hear the prompt spoken aloud.
.PARAMETER Message
The text to display. Keep it short. The command will throw an exception if the
message length is greater than 800.
.PARAMETER Button
The button set to display. The default is OKOnly. Possible values are:
OkOnly
OkCancel
AbortRetryIgnore
YesNoCancel
YesNo
RetryCancel
.PARAMETER Icon
The icon to display. The default is Information. Possible values are:
Critical
Question
Exclamation
Information
.PARAMETER Title
The message box title. The default is no title. The title should be less than
60 characters long, otherwise it will be truncated. Shorter is always better.
.PARAMETER Passthru
Use this parameter if you want the button value to be passed to the pipeline.
.PARAMETER Voice
Use text to speech to announce the prompt. This parameter has an alias of Speak.
.EXAMPLE
.Parameter VoiceGender
The command will use the default voice. Or you can specify either a Male or Female
voice. This parameter will have no affect unless you also use -Voice. This
parameter has an alias of Gender.
.EXAMPLE
PS C:\> New-Messagebox "Time to go home!"
Display a message box with no title and the OK button.
.EXAMPLE
PS C:\> $rc= New-Messagebox -message "Do you know what you're doing?" -icon exclamation -button "YesNoCancel" -title "Hey $env:username!!" -passthru
Switch ($rc) {
"Yes" {"I hope your resume is up to date."}
"No" {"Wise move."}
"Cancel" {"When in doubt, punt."}
Default {"nothing returned"}
}
.EXAMPLE
PS C:\> New-MessageBox -message "Are you the walrus?" -icon question -title "Hey, Jude" -button YesNo -voice
Display the message box and speak the message.
.NOTES
Version : 2.0
Last Updated : 1/30/2014
Learn more:
PowerShell in Depth: An Administrator's Guide
PowerShell Deep Dives
Learn PowerShell 3 in a Month of Lunches
Learn PowerShell Toolmaking in a Month of Lunches
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
"Those who forget to script are doomed to repeat their work."
.LINK
https://jdhitsolutions.com/blog/
.INPUTS
None
.OUTPUTS
[system.string]
#>
[cmdletbinding()]
Param (
[Parameter(Position=0,Mandatory=$True,
HelpMessage="Specify a display message or prompt for the message box",
ValueFromPipelinebyPropertyName=$True)]
[ValidateNotNullorEmpty()]
[Alias("prompt")]
[ValidateScript({
if ($_.length -gt 800) {
Throw "Keep the message to less than 800 characters"
}
else {
$True
}
})]
[string]$Message,
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[ValidateSet("OkOnly","OkCancel","AbortRetryIgnore","YesNoCancel","YesNo","RetryCancel")]
[string]$Button="OkOnly",
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[ValidateSet("Critical", "Question", "Exclamation", "Information")]
[string]$Icon="Information",
[ValidateScript({
if ($_.length -gt 60) {
Throw "Keep the title to less than 60 characters"
}
else {
$True
}
})]
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[string]$Title,
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[switch]$Passthru,
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[Alias("Speak")]
[switch]$Voice,
[Parameter(ValueFromPipelinebyPropertyName=$True)]
[ValidateSet("Male","Female")]
[Alias("Gender")]
[string]$VoiceGender
)
Write-Verbose "Starting $($myinvocation.MyCommand)"
Write-Verbose "Title = $Title"
Write-Verbose "Icon = $icon"
Write-Verbose "Button = $Button"
Write-Verbose "Message = $Message"
Try {
Write-Verbose "Loading VisualBasic assembly"
#load the necessary assembly
Add-Type -AssemblyName "Microsoft.VisualBasic" -ErrorAction Stop
if ($voice) {
Write-Verbose "Loading speech assembly"
Try {
#load the necessary assembly
Add-Type -assembly system.speech -ErrorAction Stop
$synth = new-object System.Speech.Synthesis.SpeechSynthesizer -ErrorAction Stop
if ($VoiceGender) {
Write-Verbose "Selecting a $voiceGender voice"
$synth.SelectVoiceByHints($VoiceGender)
}
else {
Write-Verbose "Using default voice"
}
$synth.SpeakAsync($message) | Out-Null
}
Catch {
Write-Warning "Failed to add System.Speech assembly or create the SpeechSynthesizer object."
Write-Warning $error[0].Exception.Message
#bail out
Return
}
} #if $voice
#create the message box using the parameter values
#Whatever button the user clicks will be the return value
$returnValue = [microsoft.visualbasic.interaction]::Msgbox($message,"$button,$icon",$title)
}
Catch {
Write-Warning "Failed to add Microsoft.VisualBasic assembly or create the messagebox."
Write-Warning $error[0].Exception.Message
}
#write return value if -Passthru is called
if ($Passthru) {
Write-Verbose "Passing return value from message box"
Write-Output $returnValue
}
Write-Verbose "Ending $($myinvocation.MyCommand)"
} #end function
#set an optional alias
Set-Alias -name nmb -Value New-Messagebox
该版本经过调整以包含更好的参数验证。我还修改了它,以便参数接受按属性名称输入的管道。我认为这个函数有很好的文档记录,所以我不会花太多时间讨论它。你可以自己尝试一下。请务必使用错误的参数值进行测试,以便您可以了解验证的工作原理。
如果您使用的是此功能的旧版本,则有一个重大更改。过去,我认为该函数总是将单击的按钮值写入管道。现在,除非您使用 -Passthru,否则该函数不会向管道写入任何内容。当您运行这样的命令时:
new-messagebox -Title "Friday Fun" -Message "Do you think PowerShell is fun?" -Icon Question -Button YesNo
你会得到这个:
因为我没有使用 -Passthru,所以不会将任何内容写入管道。现在是额外有趣的部分。如果您通读代码清单,您可能会看到对语音程序集的引用。我想,为什么不包括一个大声朗读信息的选项呢?因此,我使用 System.Speech.Synthesis.SpeechSynthesizer 类来朗读消息框中的文本。使用 -Voice 参数将其打开。 PowerShell 将使用系统的默认语音。但等等,还有更多!我还添加了第二个参数 VoiceGender,以便您可以指定是否需要男声或女声。在后来的客户端操作系统上,我相信您会自动获得其中之一。
单击此处查看完整尺寸的视频或右键单击并保存视频剪辑。
这是我运行的示例脚本。
#requires -version 3.0
#dot source the New-MessageBox function
. C:\scripts\New-MessageBox.ps1
$r = New-Messagebox -Message "Are you the walrus?" -Title "Hey Jude" -Icon Question -Button YesNo -Voice -VoiceGender Female -Passthru
If ($r -eq "Yes") {
Write-Host "Excellent! goo goo g'joob." -ForegroundColor Green
}
else {
$r = New-Messagebox -Message "Are you the eggman?" -Title "Hey Jude" -Icon Question -Button YesNo -Voice -VoiceGender Female -Passthru
if ($r -eq "Yes") {
Write-Host "Excellent! They are the eggmen." -ForegroundColor Green
}
else {
Write-Host "Bummer. Let it be." -ForegroundColor Yellow
}
}
享受!
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag