Search code examples
.net-corepowershell-7.0

Powershell, standalone OK, but scripted output is just newlines


Summary

I am baffled that typing in my code in PS7 works like a charm, but when I put it in a script I seem to NOT get any output.

Info

Take a look at the following script, which runs without generating any errors:

"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

The output that it generates looks like this: Scripted:output are just newlines

But here's the thing when I type the same statements in powershell:

Manual: this is what I need

So obviously I am missing something here... Using powershell 7 and my app which is written using C#/.NET core 5

Question

How do I get to fix this, and what is the cause of this behavior?


Solution

  • I finally Found a fix, though I am unsure why this fixes the error.

    when I change the lines

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

    into

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

    then the staticdefs suddenly also generates output

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

    So this a partial answer, it tells how to fix/work around the problem, but it doesn not answer why it's behavior is diffrently from the command line and script.

    Resulting in the following screenshot that shows the output that I actually expected:

    Fixed output