【发布时间】:2022-01-22 23:04:39
【问题描述】:
PS> gci C:\
<<here it doesn't print the value of $? as 'true' after command executes>
PS> $?
true
但是,当我尝试使用自己的 powershell 函数重新创建此行为时,我得到:
PS> function test1 { write-host "hello"; return $true}
PS> test1
hello
true
PS> $?
true
什么给了?为什么我不能从管道中隐藏函数的返回值,并且只将它写入 $?变量?
【问题讨论】:
-
试试
function test1 { write-host "hello";} -
你想解决什么问题?
-
$?包含最后一个命令的执行状态。这里实际上没有问题。如果你想看到它是假的,你可以尝试类似
gci some/random/path/,然后你可以输入$?,你会遇到它是假的 -
你也可以使用
Write-Verbose让你的命令正常运行,但是如果你需要通过添加-Verbose来显示一些有用的输出。
标签: powershell