当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:消息框作家

作者:精品下载站 日期: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
    }
}

享受!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯