从 Powershell 5 移植到 Powershell 7 的功能因“未将对象引用设置为对象的实例"而失败 [英] Function ported from Powershell 5 to Powershell 7 fails with "object reference not set to an instance of an object"

查看:88
本文介绍了从 Powershell 5 移植到 Powershell 7 的功能因“未将对象引用设置为对象的实例"而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)
}

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

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.

我试图在 body 中声明参数(未将对象引用设置为对象的实例),希望可能存在一些语义差异,但这并没有帮助.

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.

推荐答案

这是一个已知问题,原因是 .NET Core 中不存在所需的某些功能,而 PS v7 是在该内核上构建的.看起来像是提出了一个错误,但没有修复就关闭了:

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 方法抛出对象引用未设置为对象."在 PowerShell 6.1.2 中

这篇关于从 Powershell 5 移植到 Powershell 7 的功能因“未将对象引用设置为对象的实例"而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆