2

Following code works as expected in 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)
}

However in powershell 7.1.0 (preview 5) execution is failing with "object reference not set to an instance of an object" and I'm actually lost. Googling does not help much since it looks like this error can be encountered in very different context. So, PowerShell experts, a question to you, how can I fix this.

enter image description here

I've tried to declare params inside body (object reference not set to an instance of an object) in a faint hope that might be there's some semantic difference but that hadn't helped.

shabunc
  • 23,119
  • 19
  • 77
  • 102

1 Answers1

4

This is a known issue and is due to the fact that some of the functionality required is not present in .NET Core, which PS v7 is built on. Looks like a bug was raised but it was closed without a fix:

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

boxdog
  • 7,894
  • 2
  • 18
  • 27
  • Gosh! My initial understanding was that Microsoft heavily invest in Powershell but that contradicts to my experience of using it. It’s honestly quite unstable:( – shabunc Jul 10 '20 at 14:13
  • Microsoft do invest heavily in PowerShell, but to aid with the effort of making it cross-platform, they based the newer versions on .NET Core, which is a subset of .NET that works on Windows, Linux and Mac. I imagine their priority is to port the most widely used functionality first and work on more rarely-used stuff (like SpeechSynthesizer) later (if at all). As a product, I don't think it's unstable at all, just that the move away from Windows-exclusivity has limited what they can do immediately, so you need to take care between versions. – boxdog Jul 10 '20 at 14:21
  • well, for end user it looks like specific component has been working and after update just stopped. Not just stopped but fails with a cryptic message. On the same machine and without updating OS. – shabunc Jul 10 '20 at 14:30
  • Also, you may want to pay very close attention to the word `Preview`. That actually means you should see some type of instability and that it is not ready for production use yet. Just wanted to point that out. – Patrick Mcvay Jul 10 '20 at 14:33
  • @PatrickMcvay that’s a valid not, can not argue with that. – shabunc Jul 10 '20 at 14:44