This script is useful in more complex processes where you want to check if the server is online before attempting to execute something remotely or connecting to the SQL database. Many commands do not handle timeouts well and this test will keep things moving if there is a down server.
This is a simple one server test using the test-connection cmdlet to check if the server is running and returns a $True or $False. Note: notice the “-Quiet” switch
## Ping test but do NOT display each result
Test-Connection -TargetName 'MySQLServer01' -Quiet
Check If ‘Multiple’ Servers Are Online
Most networks have a few more servers than one. Luckily we can test multiple servers using the same statement. A comma separated list can be used for a handful of servers quickly.
We removed the -Quiet switch as we want to see the Count results.
# Test multiple servers, 3 times each
Test-Connection -TargetName 'Server1','10.10.1.3','Server2' -Count 3
We could create a giant script to test each server in the environment using just the “one server test” or the comma separated list but this is not very sustainable. In a growing network we would need to modify the script over time copying and pasting. Or we can list the servers in an array.
Test-Connection To Multiple Computers
To test multiple computers we need a list or in this case we are using an array. This example keeps everything in the same Powershell script and bring the code down to only 1 line for testing.
Since the Test-Connection command returns a logical $True or $False we can use a simple IF statement to return results.
# Add server name or IP address to array
$servers = @(
'Server1',
'10.10.1.3',
'Server2' )
foreach($server in $servers) {
if(Test-Connection -TargetName $server -Quiet) {
write-host 'success!'
} else {
write-host 'Failed'
}
}
Server List (Object) From Database
The most efficient method to maintain your network server list is in a database table where it can be manually or automatically updated by another tool or process. I use the Invoke-Sqlcmd command to query the severs into an object ($servers) and then use this in place of the array with the query field name specified (ServerName).
## get list of servers from query
$query = "USE AdminDB; SELECT DISTINCT ServerName FROM AllServers"
$servers = invoke-sqlcmd -ServerInstance MyUtilityServer01 -Query $query
foreach($server in $servers) {
if(Test-Connection -TargetName $server.ServerName -Quiet) {
write-host 'success!'
} else {
write-host 'Failed'
}
}
More Details
Test-Connection has a few more command switches where we can collect more data on our connections. The following script will provide the Traceroute Hops using the IPv4 protocol with a timeout of 2 seconds and testing each connection once (db tales com). We also removed the -Quiet command since we want to see the Hop results in case there is a network issue to be found.
$servers = @(
'Server1',
'10.10.1.3',
'Server2' )
foreach($server in $servers) {
Test-Connection -TargetName $server -Traceroute -IPv4 -TimeoutSeconds 2
}