PowerShell one-liner: Test connectivity

In this one-liner series I would like to show you several ways to test connectivity. There are lots of methods and basic commands to check network connection like ping, telnet, tracert etc. However it this post you will find out how to combine other PowerShell commands into one-liner script.

Most common PowerShell command is Test-Connection which returns basically the same output as Ping.

test-connection
test-connection

Test-Connection -ComputerName DC01 -Count 10 -ErrorAction SilentlyContinue | 
Select-Object Address,ProtocolAddress,ResponseTime 

To use it for a bunch of our servers we can first create a list of our servers in txt file and then test connectivity. Additionally I use Out-GridView at the end to display results in the real time:

 
Get-Content -Path "C:\Users\pawel.janowicz\desktop\servers.txt" | 
% { Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue | 
Select-Object Address,ProtocolAddress,ResponseTime} | 
Out-GridView -Title "Results"

As you probably saw in previous articles there is also an easy way to get secure channel name remotely using nltest command:

Invoke-Command DC01 -ScriptBlock{nltest.exe /sc_verify:$((Get-ADDomain).DNSRoot) | 
Where-Object {$_ -match "Trusted DC Name"} | 
ForEach {$_.trim().Substring(18)} } 

Now we can try to test connectivity using above example – Get secure channel name remotely and in the same line check if its “pingable”:

Invoke-Command DC01 -ScriptBlock{nltest.exe /sc_verify:$((Get-ADDomain).DNSRoot) | 
Where-Object {$_ -match "Trusted DC Name"} | 
ForEach {Test-Connection $_.trim().Substring(18) -Count 4 } } -Credential pawel.janowicz | 
Format-Table address,buffersize,replysize,responsetime

Another useful command is Test-NetConnection where we can specify port number. Below you will see how to get all Domain Controllers in your domain and then check connection on port 636:

# Get your ad domain
$DomainName = (Get-ADDomain).DNSRoot
 
# Get all DC's
$AllDCs = Get-ADDomainController -Filter * -Server $DomainName 

# Using one-liner - Get all DC's and test connection on port 636
(Get-ADDomainController -Filter * -Server $((Get-ADDomain).DNSRoot)).Hostname | 
Foreach{Test-NetConnection -ComputerName $_ -Port 636 -InformationLevel Detailed} | 
Out-GridView -Title "Results"

If you would like to see other articiles about testing connection please check below links:
Testing connection to secure channel
Test connection to servers on several ports

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.