抑制 Test-NetConnection 的信息 [英] Suppress information of Test-NetConnection
问题描述
如何抑制来自 Test-NetConnection -ComputerName localhost
的 PowerShell 会话的详细信息?
How can I supress the detailed information from Test-NetConnection -ComputerName localhost
for a PowerShell session?
Test-NetConnection
将相当多的信息写入显示在控制台顶部的控制台,覆盖进度条.
Test-NetConnection
writes quite a bit of information to the console which is displayed in the top of the console, overwriting the progress bar.
可以通过将 看起来可以像使用 -InformationLevel
设置为 Quiet
来抑制输出-InformationLevel
参数一样抑制输出,但事实证明,即使使用 -InformationLevel
到 Quiet
中的信息显示控制台顶部.
The output can be suppressed by setting the
It looked like the output could be supressed like with the -InformationLevel
to Quiet
like so-InformationLevel
parameter, however it turns out that even with -InformationLevel
to Quiet
the information in the top of the console is shown.
Test-NetConnection -ComputerName localhost -InformationLevel Quiet
我想为整个脚本设置一次 InformationLevel
,就像你会 在使用 Invoke-WebRequest
时抑制进度条.
I would like to set the InformationLevel
just once for the whole script, like you would suppress the progress bar when using Invoke-WebRequest
.
$progressPreference = 'silentlyContinue' # Subsequent calls do not display UI.
Invoke-WebRequest ...
$progressPreference = 'Continue' # Subsequent calls do display UI.
据我所知,没有类似的 $InformationLevelPreference
Test-NetConnection 会监听.
To my knowledge there is no similar $InformationLevelPreference
Test-NetConnection will listen to.
推荐答案
Ansgar 提议的解决方案的替代方案是使用 $PSDefaultParameterValues
自动变量:
An alternative to Ansgar's proposed solution would be to use the $PSDefaultParameterValues
automatic variable:
PS C:\> Test-NetConnection localhost
ComputerName : localhost
RemoteAddress : ::1
InterfaceAlias : Loopback Pseudo-Interface 1
SourceAddress : ::1
PingSucceeded : True
PingReplyDetails (RTT) : 0 ms
PS C:\> $PSDefaultParameterValues['Test-NetConnection:InformationLevel'] = 'Quiet'
PS C:\> Test-NetConnection localhost
True
$PSDefaultParameterValues
字典中的条目具有CommandName:ParameterName
形式的键,然后值是您要默认提供的参数参数值.如有必要,您可以将通配符合并为 CommandName
部分的一部分
Entries in $PSDefaultParameterValues
dictionary have keys in the form of CommandName:ParameterName
, and then the value is the parameter argument value you want to supply by default. You can incorporate wildcards as part of the CommandName
part if necessary
您可能在切换到 -InformationLevel:Quiet
时看到进度条消失的原因是,向环回接口发出单个 ICMP 请求并返回单个布尔值发生得如此之快,以至于宿主应用程序永远没有机会实际获取进度流状态更改并在返回提示之前显示进度叠加.
The reason that you might have seen the progress bar go away when switching to -InformationLevel:Quiet
is that issuing a single ICMP request to a loopback interface and returning a single boolean value happens so fast that the host application never has a chance to actually pick up the progress stream state change and display the progress overlay before returning to the prompt.
如果你想在执行过程中抑制顶部的进度条(不管命令运行多长时间),使用 $ProgressPreference
变量:
If you want to suppress the progress bar at the top during execution (regardless of how long the command is running), use the $ProgressPreference
variable:
PS C:\> $ProgressPreference = 'SilentlyContinue'
PS C:\> Test-NetConnection localhost
这篇关于抑制 Test-NetConnection 的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!