Ping Active Directory Servers with PowerShell

By | 2016-12-29

Using the ping command to test for responses from network computers is a quick way to see if a network path is open between two hosts. Not in depth, but basic. Here is how to get a list of servers from Active Directory, and ping them with PowerShell.

This is another basic script to show how to grab a list of computers from Active Directory from a specific organizational unit (OU), store them in a list, then ping them. I’ve been asked for this knowledge more than once, so now it has become a post that I can point people at.

The first thing the script does is load the Active Directory module. This module makes getting a list of machines from AD much easier.

Next, we use the Get-ADComputer cmdlet to grab a list of computer information, specifically from the Servers OU. This is specified in the SearchBase parameter, by using the distinguished name of the OU. The list is stored in the variable $HostList.

Now that the list is acquired, a simple ForEach loop is used to roll through the list. Each entry is checked with the Test-Connection cmdlet. If the remote host responds, a “responding” message is written to the screen. If not, then a “not responding” message is written.

Here is the script:

import-module ActiveDirectory

$HostList=Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=labps,DC=local" -SearchScope Subtree

ForEach($Server in $HostList)
{
    $HostToPing=$Server.DNSHostName
    if(test-connection -computername $HostToPing -count 1 -quiet )
    {
      write-host "$HostToPing is responding"
    }Else{
      write-host "$HostToPing is not responding"
    }
}

And a sample output:

server01.labps.local is not responding
server02.labps.local is responding
server03.labps.local is responding
server04.labps.local is responding
server05.labps.local is responding
server06.labps.local is responding
server07.labps.local is responding
server08.labps.local is responding
server09.labps.local is responding

This is just a basic example, and could be expanded on quite a bit. By expanding just a bit, you could potentially have a basic uptime testing script.

For example, you could use the Task Scheduler to run the script every 5 minutes, and in the “not responding” section you might use the send-mailmessage cmdlet to send an email when a host is not responding.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

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.