powershell - 对 Powershell 脚本中的所有错误调用函数

$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/

相关文章:

c++ - 自动占位符类型和显式模板类型在 C++20 中是否等效?

websocket - 将 Nextjs 部署到 Vercel 的 SocketIO,套接字未连接

python - 小列表的长排列

c# - 如何将字符串拆分为包含前一个最后一个字符的两个字符的字符串数组?

r - 有没有一种方法可以将列添加到函数形式的数据框中

javascript - 如何在 react 中更改 useState 的对象值?

c++ - std::unordered_map 如何确定哈希表中特定键的位置?

firebase - argumnet 类型 'Null Funcion(DataSnapshot)

r - 如何将多列连接成一列并删除重复项?

c++ - 这个 'for' 循环与键盘记录器有什么关系?