PowerShell Write-Output inside "if" doesn't output anything

I have been studying PowerShell lately and have found behavior that I cannot understand. Let me clarify the code below:

function func1()
{
    Write-Output "output from func1()"
    func2
    return $true
}

function func2()
{
    Write-Output "output from func2()"
    func3
}

function func3()
{
    Write-Output "output from func3()"
}

Write-Output "*** 1. run alone ****"
func1

Write-Output "*** 2. run inside if ****"
if (func1) {
    #do nothing
}

It is strange when func1 is called directly, it can display a message as expected, but if you enter the "if" statement inside, it will not. See Output as shown below:

*** 1. run alone ****
output from func1()
output from func2()
output from func3()
True
*** 2. run inside if ****

You can see it empty. Why is this, and how can I include output as the first example? Thank!

+5
source share
2 answers

PowerShell : . Write-Output, , func1() String, Boolean. :

$return_values = func1;
$return_values | Get-Member

, System.String System.Boolean $return_values. , return "exit" "break" : , , (+ return).

- Write-Output Write-Host, .

+4

, Write-Output Write-Host. , , , Write-Host stdout.

SO: : "Write-Host" , "Write-Output" , "Write-Output" , []:: "?

+2

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


All Articles