如何禁止Test-NetConnection -ComputerName localhostPowerShell会话提供详细信息?
Test-NetConnection 将大量信息写入控制台,该信息显示在控制台顶部,覆盖进度条。
输出可以通过设置能够抑制
它看起来像输出可以supressed像与-InformationLevel到Quiet喜欢所以-InformationLevel参数,但事实证明,即使-InformationLevel以Quiet在控制台的顶部的信息被示出。
Test-NetConnection -ComputerName localhost -InformationLevel Quiet
Run Code Online (Sandbox Code Playgroud)
我想InformationLevel为整个脚本设置一次,就像您使用时抑制进度条一样Invoke-WebRequest。
$progressPreference = 'silentlyContinue' # Subsequent calls do not display UI.
Invoke-WebRequest ...
$progressPreference = 'Continue' # Subsequent calls do display UI.
Run Code Online (Sandbox Code Playgroud)
据我所知,没有类似的$InformationLevelPreferenceTest-NetConnection将收听。
Ansgar提出的解决方案的替代方法是使用$PSDefaultParameterValues自动变量:
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
Run Code Online (Sandbox Code Playgroud)
$PSDefaultParameterValues字典中的条目具有形式为的键CommandName:ParameterName,然后该值是默认情况下要提供的参数实参值。CommandName如有必要,您可以将通配符作为该部分的一部分
切换到-InformationLevel:Quiet该选项时,您可能会看到进度条消失的原因是,向环回接口发出单个ICMP请求并返回单个布尔值发生的速度如此之快,以至于宿主应用程序永远没有机会实际获取进度流状态更改,并在返回提示之前显示进度叠加。
如果要在执行期间取消进度条的顶部(无论命令运行多长时间),请使用$ProgressPreference变量:
PS C:\> $ProgressPreference = 'SilentlyContinue'
PS C:\> Test-NetConnection localhost
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3095 次 |
| 最近记录: |