Powershell,独立 OK,但脚本输出只是换行符

Powershell, standalone OK, but scripted output is just newlines

总结

我感到困惑的是,在 PS7 中输入我的代码非常有效,但是当我将它放入脚本中时,我似乎没有得到任何输出。

信息

看看下面的脚本,它运行时没有产生任何错误:

"Iapetus elementary test script"
#Before doing anything ensure we are at least at powershell v6
#Official Powershell updates can be found here: https://aka.ms/powershell-release?tag=stable
if ($host.Version.Major -lt 6)
{
  throw "Minimum powershell version for this script is v6. Detected: "+$host.Version
}

#ensure colors are default to avoid printing black on black
#[Console]::ResetColor()

#clear the output screen
clear

# Locate the Iapetus core DLL
$IapetusBinFolder=$PSScriptRoot+"\..\bin\AnyCPU\Debug\net5.0"
$CoreDLL="SNG.Iapetus.Core.dll"
$IapetusCoreDLLPath=$IapetusBinFolder+"\"+$CoreDLL

#Add the Iapetus library to the powershell environment
"Loading "+$IapetusCoreDLLPath
Add-Type -Path $IapetusCoreDLLPath

#Open/Connect to the Iapetus instance
$instance=[SNG.Iapetus.Core.Iapetus]::Instance
"Iapetus instance loaded:"
$instance

# check out some of the static definitions
$staticdefs=$instance.StaticDefinitions;
"Iapetus.StaticDefinitions:"
$instance.StaticDefinitions | Get-Member

它生成的输出如下所示:

但是当我在 powershell 中键入相同的语句时,情况是这样的:

很明显我在这里遗漏了一些东西...使用 powershell 7 和我使用 C#/.NET core 5 编写的应用程序

问题

我该如何解决这个问题,这种行为的原因是什么?

我终于找到了修复程序,虽然我不确定为什么这会修复错误。

当我换台词时

#Open/Connect to the Iapetus instance
$instance=[SNG.Iapetus.Core.Iapetus]::Instance
"Iapetus instance loaded:"
$instance

进入

#Open/Connect to the Iapetus instance
$instance=[SNG.Iapetus.Core.Iapetus]::Instance
"Iapetus instance loaded:"
$instance | Get-Member

然后 staticdefs 突然也生成输出

$staticdefs=$instance.StaticDefinitions;
"Iapetus.StaticDefinitions:"
$staticdefs | Get-Member

所以这是一个部分答案,它告诉我们如何fix/work解决这个问题,但它没有回答为什么它的行为不同于命令行和脚本。

导致以下屏幕截图显示了我实际预期的输出: