如何让 Windows PowerShell 在 .bat 作业运行完成后播放声音?

问题描述 投票:0回答:4

正如标题所述,我有一个

.bat
作业在 PowerShell 中运行,运行完成后,我希望发出声音通知。我想知道是否有一个 PowerShell 命令可以添加到我现有的 PowerShell 命令中。

powershell audio notifications beep
4个回答
37
投票

除了 @TheGameiswar 建议的解决方案之外,您还可以通过让系统实际与您对话来获得一些乐趣:

# Create a new SpVoice objects
$voice = New-Object -ComObject Sapi.spvoice

# Set the speed - positive numbers are faster, negative numbers, slower
$voice.rate = 0

# Say something
$voice.speak("Hey, Harcot, your BAT file is finished!")

注意:我只在Windows 10上测试过这个,所以它可能不适用于其他版本,但请尝试一下。


18
投票

除了boxdog这里)和TheGameiswar这里)的优秀解决方案之外,我想提一下另一种可能性,它可以让你播放一些标准的系统声音:

[System.Media.SystemSounds]::Asterisk.Play()
[System.Media.SystemSounds]::Beep.Play()
[System.Media.SystemSounds]::Exclamation.Play()
[System.Media.SystemSounds]::Hand.Play()
[System.Media.SystemSounds]::Question.Play()

另一种文本转语音方法

Add-Type -AssemblyName System.Speech
$synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synth.Speak("Hey $env:USERNAME, your job is finished!")

定制

有关完整详细信息,请阅读文档

语音

选择声音:

$synth.SelectVoice("Microsoft Zira Desktop")

您可以通过以下方式查看可用语音:

$synth.GetInstalledVoices() | Select-Object -ExpandProperty VoiceInfo

价格

将语速设置为 -10(慢)到 10(快):

$synth.Rate = 5

音量

将音量设置为 0(安静)到 100(大声):

$synth.Volume = 75

13
投票

您可以使用powershell自动变量来检查bat文件状态..根据this

$?
返回true,如果命令成功..

下面是示例代码

$a =Invoke-Command -ScriptBlock {

& "C:\temp1\test.bat"
}

if($?){

[console]::beep(500,300)

}

您还可以播放自定义声音,

$PlayWav=New-Object System.Media.SoundPlayer

$PlayWav.SoundLocation=’C:\Foo\Soundfile.wav’

$PlayWav.playsync()

参考资料:
https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-play-wav-files/


0
投票

我喜欢“另一种文本转语音方法”版本。谢谢。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.