fortypoundhead.com

Ping Active Directory servers with PowerShell

Posted On 2016-12-29 by dwirch
Keywords:
Tags: Powershell Scripting Tutorial Windows
Views: 566


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.


About the Author

dwirch has posted a total of 174 articles.

You can find more information from dwirch by visiting http://www.derekwirch.com.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.156.39.44

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News

Fold Code Manager into main KB?
dwirch posted on July 21, 2017 at about 22:46 in Site News

Fold Code Manager into main KB?
dwirch posted on July 20, 2017 at about 7:55 in Site News

Job Spammer: Sam Mallon
dwirch posted on July 18, 2017 at about 18:36 in Spammers