Write-Error is not output to the console until the script exits

Say you have two scenarios,

outer.ps1

function Main()
{
    Write-Host "starting outer"
    TryRun "outer" {
        & $PSScriptRoot\inner.ps1
    }
}

Main

and inner.ps1

function ThrowError()
{
    throw "Error"
}

function Main()
{
    Write-Host "starting inner"
    TryRun "inner" { 
        ThrowError 
    }
}

Main

where is TryRundetermined to be the same in both and is defined as:

function TryRun([string] $name, [scriptblock] $action)
{
    try
    {
        $action.Invoke()
        Write-Host "'$name' successfully finished"

        Exit 0
    }
    catch [System.Exception]
    {
        $separator = New-Object string @("-", 120)
        Write-Error "'$name' failed`n$separator`n$($_.Exception.ToString())`n$separator"

        Exit 1
    }
}

Now the problem is that when executing the outer.ps1output:

starting outer
starting inner
<exception should have been printed here, but it was not>
'outer' successfully finished

However, the beginning inner.ps1directly proves that it behaves as expected:

TryRun : 'inner' failed
-------------------------------------------------------------------------------
-----------------------------------------
System.Management.Automation.RuntimeException: Error ---> 
System.Management.Automation.RuntimeException: Error
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(F
unctionContext funcContext, Exception exception)
   ...

And what's even funnier is if you change Write-Errorto Write-Hostand outer.ps1throw an exception printed

I also tried flushing the buffers Console.Out, etcbefore doing it Exit, but that didn't help

Now why is this so? What is the reason why PS does not throw an exception before exiting? A debugging session shows that it always gets caught and does thisWrite-Error

+2
1

, .invoke() . . invoke() PSObjects. , , , ; : {"a"}.invoke() "a", - ( , ?).

script &. $action.Invoke() & $action outer.ps1 .

.. .invoke() $error, - {write-error "a"}.invoke(); $error

+2

Source: https://habr.com/ru/post/1669087/


All Articles