从 Powershell 5 移植到 Powershell 7 的函数失败并显示 "object reference not set to an instance of an object"

Function ported from Powershell 5 to Powershell 7 fails with "object reference not set to an instance of an object"

以下代码在 powershell 5.1 中按预期工作:

function Get-Say([String] $Message, [String] $Voice = "Microsoft Irina Desktop") {
    Add-Type -AssemblyName System.speech
    $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $speak.SelectVoice($Voice)
    $speak.Speak($Message)
}

但是在 powershell 7.1.0(预览版 5)中执行失败并显示“对象引用未设置为对象的实例”,我实际上迷路了。谷歌搜索没有多大帮助,因为看起来这个错误可能会在非常不同的上下文中遇到。那么,PowerShell 专家,请教您一个问题,我该如何解决这个问题。

我曾尝试在 body (object reference not set to an instance of an object) 中声明参数,希望可能存在一些语义差异,但这并没有帮助。

这是一个已知问题,因为 .NET Core 中不存在所需的某些功能,PS v7 构建于此之上。似乎出现了错误,但没有修复就关闭了:

System.Speech.Synthesis.SpeechSynthesizer Speak method throws Object reference not set to an instance of an object." in PowerShell 6.1.2