小编bob*_*uan的帖子

PowerShell写入输出"if"内部不输出任何内容

我最近在学习PowerShell,发现了一个我无法理解的行为.让我在下面的代码中解释:

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
}
Run Code Online (Sandbox Code Playgroud)

直接调用func1时很奇怪,它可以按预期输出消息,但是如果放在"if"语句中,它就不会.见输出如下:

*** 1. run alone ****
output from func1()
output from func2()
output from func3()
True
*** 2. run inside if ****
Run Code Online (Sandbox Code Playgroud)

你可以看到它是空的.为什么会这样,我如何像第一个例子那样启用输出?谢谢!

powershell

8
推荐指数
2
解决办法
3820
查看次数

标签 统计

powershell ×1