Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

powershell - Suppress information of Test-NetConnection

How can I supress the detailed information from Test-NetConnection -ComputerName localhost for a PowerShell session?

Test-NetConnection writes quite a bit of information to the console which is displayed in the top of the console, overwriting the progress bar.

enter image description here

The output can be suppressed by setting the -InformationLevel to Quiet like so It looked like the output could be supressed like with the -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

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.

To my knowledge there is no similar $InformationLevelPreference Test-NetConnection will listen to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...