Sometimes it could happen that your computer lost connection to other hosts or the internet. What is the first you should do? For example, you could try to reach other hosts or your default gateway. Sure, you could use the bottom up method by climbing up the ladder of the OSI Model and check your network cable (Layer 1), climb up again (Layer 2) and check your network card or your switch or you could use the other way and climb down.
In this blog post we climb down and check the Default Gateway by using a PowerShell function. I’ve created a small function which reads the IP-Address of the Default Gateway from the routing table of the localhost. Let’s start.
Gathering Default Gateway’s IP-Address
The following gives me the IP of my Default Gateway:
Get-NetRoute -DestinationPrefix 0.0.0.0/0
Note, that it always gives me the IP of the Default Gateway, regardless of how many network cards are installed and configured. It’s called Default Route (0.0.0.0/0) or Last Gateway of Resort. Well, my Default Gateway’s IP is 192.168.0.1. Let’s proceed to the next step.
Discovering Test-Connection
In my article The modern version of ping: Test-Connection I’ve played with the cmdlet Test-Connection. Test-Connection is like ping. For the following we need Test-Connection to test the Gateway’s IP. First, we could try to ping our gateway.
Test-Connection -ComputerName 192.168.0.1
Nice. But that is not what we are looking for. We want to call the IP from the Routing Table. For this, we must expand the property of NextHop to get just the IP-Address.
Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty NextHop
So, we know that we can insert this command into Test-Connection.
Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty NextHop)
Why did I put Get-NetRoute in round brackets? Commands in round brackets are executed immediately. Therefore, when ping starts, the Gateway’s IP-Address is already there!
Great! Let’s start writing the function.
Creating a function to test the connectivity to the Default Gateway
Ok, my goal is to create a command Test-Gateway. Let’s create a function that will match my plan. I also want that the output shows only true or false.
function Test-Gateway {Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty NextHop) -Quiet}
Now the exciting moment is coming …
Test-Gateway
Hooray! It’s working.
Making it permanent
Functions you’ve created are only available in your current PowerShell Session, which means that they are gone when you close PowerShell.
If you like my function, simply insert it in your PowerShell profile. Then it will be available every time you start the best command line on this planet. Copy the following code in PowerShell to allow PowerShell running your profile file, to create the profile and to put the function in it.
Set-ExecutionPolicy remotesigned -force New-Item $profile -Force | Out-Null Add-Content $profile -Value {function Test-Gateway {Test-Connection -ComputerName (Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty NextHop) -Quiet}}
The command has now become permanent. It’s included in your profile which will load every time you start PowerShell.
notepad $profile
Close PowerShell and re-open it. Run Test-Gateway. Have fun!
Testing the Gateway on Remote Hosts
In one of my other articles I’ve shown an advanced function with remote action in it. It connects to other hosts, checks the routing table of that host and then checks if this host can reach it’s Default Gateway.
PowerShell Module
You can also save this function as a PowerShell Module which I’ve described in a previous blog post: PowerShell Functions: How to create your first PowerShell Module Command
See also
How to create PowerShell Profiles
Ping: Request timed out vs. Destination Host unreachable
Categories: PowerShell, Windows 10, Windows Server