如何将函数的值输出到调用者,但不输出到控制台 [英] How to Output value from function to caller but not to console
问题描述
function testit(){
return $ true> $ null
}
Write-Hosttesting
$ thistest = testit
Write-Hostvalue = $ thistest
当我在PowerShell脚本中使用它时,我想要在脚本中接收该值,但我不希望它显示在
如何在管道中保留返回值,但只是将其隐藏到控制台中?
如果我使用> $ null
然后它完全禁止输出 - 我只是希望它不显示在控制台中,但我仍然需要该值。
As 记录在案) PowerShell函数将所有未捕获的输出返回给调用者。如果调用者不对返回值做任何事情,PowerShell会自动将它传递给 Out-Default
,然后将它转发给 Out-Host
a href =https://technet.microsoft.com/en-us/library/gg213852.aspx =nofollow noreferrer>本文由Don Jones撰写)。
在函数内部的返回值上使用重定向操作符可以有效地抑制返回值,这样函数就不会返回任何东西。
如果你有一个像这样的函数:
function testit {
return $ true
}
并自行调用它:
testit
PowerShell隐含地执行此操作:
testit | Out-Default
有效地变为
testit | Out-Host
如果您捕获变量中的返回值
$ thistest = testit
值如果您将输出重定向或将其输入到 Out-Null $ c中,那么它将被存储在变量中,而不会在控制台上显示任何内容。
testit> $ null
testit | Out-Null
返回值将被丢弃,并且控制台上不会显示任何内容。
如果您想阻止PowerShell在管道末尾将未捕获的输出传递给 Out-Host
的默认行为,您可以通过重写 Out-Default
就像这样:
filter Out-Default {$ _ | Out-Null}
或(如@PetSerAl在评论中指出的)如下所示:
过滤默认{}
但是,请注意,此修改会为当前范围内的所有内容禁用 Out-Default
,直到您再次删除过滤器。如果您在过滤器处于活动状态时执行了 Get-ChildItem
,则除非您明确将输出写入主机控制台,否则不会显示任何内容:
Get-ChildItem | Out-Host
您可以像这样删除过滤器:
Remove-Item函数:Out-Default
Say I have this simple PowerShell function:
function testit() {
return $true > $null
}
Write-Host "testing"
$thistest = testit
Write-Host "value = $thistest"
When I use it in my PowerShell script, I want to receive the value in the script but I don't want it to show in the console.
How do I keep the return value in the pipeline but just hide it from console?
If I use the > $null
then it suppresses the output completely - I just want it to not show in the console, but I still want the value.
As documented PowerShell functions return all non-captured output to the caller. If the caller doesn't do anything with the returned value PowerShell automatically passes it to Out-Default
, which then forwards it to Out-Host
(see this article written by Don Jones).
Using redirection operators on the return value inside the function effectively suppresses the return value so that the function wouldn't return anything.
If you have a function like this:
function testit {
return $true
}
and call it by itself:
testit
PowerShell implicitly does this:
testit | Out-Default
which effectively becomes
testit | Out-Host
If you capture the return value in a variable
$thistest = testit
the value gets stored in the variable without anything being displayed on the console.
If you redirect the output or pipe it into Out-Null
testit >$null
testit | Out-Null
the return value is discarded and nothing is displayed on the console.
If you want to prevent PowerShell's default behavior of passing uncaptured output at the end of a pipeline to Out-Host
you can do so by overriding Out-Default
like this:
filter Out-Default { $_ | Out-Null }
or (as @PetSerAl pointed out in the comments) like this:
filter Out-Default {}
However, beware that this modification disables Out-Default
for everything in the current scope until you remove the filter again. If you do for instance a Get-ChildItem
while the filter is active nothing will be displayed unless you explicitly write the output to the host console:
Get-ChildItem | Out-Host
You remove the filter like this:
Remove-Item function:Out-Default
这篇关于如何将函数的值输出到调用者,但不输出到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!