$player = New-Object System.Media.SoundPlayer "C:\Error-Sound.wav"
$ErrorActionPreference = "$player.Play()"
我的代码不工作 当它在整个 Powershell 脚本中调用函数时遇到任何错误时,我想要这个 有什么帮助吗?
最佳答案
您可能希望在脚本失败时播放声音,如果是这样的话:
$ErrorActionPreference = 'Stop'
$player = New-Object System.Media.SoundPlayer "C:\Error-Sound.wav"
try
{
# Script do something here
}
catch
{
$player.PlaySync() # Play a sound if Error
Write-Warning $_.Exception.Message # Display exception in the console
}
https://stackoverflow.com/questions/70583798/