.bat作业完成运行后,如何使Windows Powershell播放声音?

时间:2019-05-08 01:48:09

标签: powershell notifications

正如标题所述,我有一个.bat作业在powershell中运行,当完成运行后,我希望发出声音通知。我想知道是否可以将Powershell命令添加到现有的powershell命令中。

2 个答案:

答案 0 :(得分:2)

除了@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上进行了测试,因此它可能无法在其他版本上运行,但请尝试一下。

答案 1 :(得分:0)

您可以使用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/