Ping servers with DNS suffix

Greetings,
I am currently using a script to check the status of a list of host names from a txt file.
However not all devices have the same domain name
Server.Prod.foo.com” or “Server.Prod.foobar.com
is there a way i can append the script i currently have to include different DNS suffixes?
Below is my current script which works fine but looking to add the above.


$ErrorActionPreference = silentlycontinue
$servers = Get-Content C:\Users\MYNAME\serverlist.txt


foreach ($_ in $servers) {
$pingtest = Test-Connection -ComputerName $_ -Count 1 -Delay 10 -TimeToLive 10
if ($pingtest.ResponseTime -gt 0)
{"$_ Is Alive" | Out-File -FilePath C:\Users\MYNAME\iplist.csv -Append -Force}
else {"$_ Is Down" | Out-File -FilePath C:\Users\MYNAME\iplist.csv -Append -Force }
}

If I understand this correctly, your list of servers only contains the NETBIOS name as in “server”, not “server.domain.com”. One thing you can do is add the additional DNS suffixes to the network adapter configuration. Adapter/TCPIP-4 Properties/Advanced/DNS/Append these DNS Suffixes.

Correct the list are not FQDN…
How would i go about adding that? " Adapter/TCPIP-4 Properties/Advanced/DNS/Append these DNS Suffixes."

You would need admin. Hit the WindowKey+R (run) and enter “ncpa.cpl”. Right mouse on your active network adapter, select Properties. Double Click on "Internet Protocol Version 4 (TCP/IP). Select Advanced. Select the DNS tab. Select the Radio Button for “Append these DNS suffixes…” then Select add and add your suffixes.

I realized this is technically not PowerShell related, no flames please :slight_smile:

2 Likes

I see so it wouldn’t be added to the script i have listed above? This would be a change technically?

I was kinda looking for a way to possible add them to an array and loop through with the script.

Again let me try to clarify, The script it to be run against hundreds of serves to obtain the status. Some of them not always ending with the same domain so i would like to run the list of server names thought the script with out adding the suffix to each and everyone in the list. Since the list changes frequently. The names would be looped thought with one domain name if non existed then use the next dns suffix. I’m no looking to changes the network adapter or any of the configurations on my end. Just need help with the syntax of powershell.
Thanks in advance!
etc…

K2Hunter, first let me advise you not to use $_ as a named variable. If you want to reference the automatic variable $_, use a ForEach-Object loop instead. Next, your plan to ping every possible domain with every host sounds like a bad approach. Especially when running sequentially, this could take forever. Can you not query for the actual hosts FQDN? If you insist one continuing down this path you should probably look into jobs, runspaces, or ForEach-Object -Parallel (powershell core only).

Here is a simple way you could test each host with each domain. However the way the logic is currently written each host will be output to the log once per domain.

$ErrorActionPreference = silentlycontinue
$ServerList = Get-Content C:\Users\MYNAME\serverlist.txt
$DomainList = 'Prod.foo.com','Prod.foobar.com'

$results = foreach ($Server in $ServerList) {
    
    foreach($Domain in $DomainList){

        $combined = $Server,$Domain -join '.'

        if(Test-Connection -ComputerName $combined -Count 1 -Delay 10 -TimeToLive 10){
            "$combined Is Alive"
        }
        else{
            "$combined Is Down"
        }
    }
}

$results | Out-File -FilePath C:\Users\MYNAME\iplist.csv
2 Likes

I see the logic you have here it seems to me sufficient to do what i was planning.
However I’d like to learn more as to what you mean with this?

Blockquote

first let me advise you not to use $_ as a named variable. If you want to reference the automatic variable $_, use a ForEach-Object loop instead.

Blockquote

Secondly I only have 3 domains to search on with old servers being phased out and new ones being added I can’t keep up with what domain they will be on when created.

I’m jsut starting to begin scripting with powershell so I appreciate your help!

So, the solution I proposed is a configuration on the network adapter for the SYSTEM running the script. Make the change there once and you are done, you don’t need to add any logic/code to the script. Doug is a PowerShell black belt (I am far from that) and he has proposed a script based solution. I would use what he has proposed as long as performance does not become an issue.

Look at my Test-ConnectionAsync function in my module PoshFunctions on the PowerShell gallery.

Test-ConnectionAsync -ComputerName server_dc1,server_dc2 -Timeout 1000

ComputerName IPAddress    BufferSize  Result ResponseTime
------------ ---------    ----------  ------ ------------
server_dc1   192.168.1.31         32 Success            0
server_dc2   192.168.1.41         32 Success            0

You can asynchronously ping many, many ComputerName at one time. Good luck!

Or if you want to test a whole network segment for connectivity you can use use the Test-Network command. It will return only those addresses that have a reverse DNS lookup, or have a successful ping. Any ip address that doesn’t have a reverse DNS lookup and doesn’t respond to ping will NOT show up in the output.

Test-Network -Subnet '192.168.1.1/24'

IpAddress     ComputerName  Result
---------     ------------  ------
192.168.1.1   UNKNOWN      Success
192.168.1.2   UNKNOWN      Success
192.168.1.3   hostfqdn     Success
192.168.1.4   hostfqdn     Success
192.168.1.5   UNKNOWN      Success
192.168.1.6   hostfqdn     TimeOut
192.168.1.9   hostfqdn     Success
192.168.1.10  hostfqdn     Success
192.168.1.11  hostfqdn     Success
192.168.1.12  hostfqdn     Success
192.168.1.15  hostfqdn     TimeOut
192.168.1.16  hostfqdn     Success
192.168.1.17  UNKNOWN      Success
192.168.1.18  hostfqdn     Success
192.168.1.19  hostfqdn     Success
192.168.1.20  hostfqdn     TimeOut
192.168.1.21  hostfqdn     TimeOut
192.168.1.22  hostfqdn     Success
192.168.1.25  hostfqdn     Success
192.168.1.26  hostfqdn     TimeOut
192.168.1.27  hostfqdn     Success
192.168.1.28  hostfqdn     Success
192.168.1.30  hostfqdn     Success
192.168.1.31  hostfqdn     Success
192.168.1.32  hostfqdn     Success
192.168.1.33  hostfqdn     Success
192.168.1.35  hostfqdn     Success
192.168.1.36  hostfqdn     Success
192.168.1.37  hostfqdn     Success
192.168.1.39  hostfqdn     Success
192.168.1.40  hostfqdn     Success
192.168.1.41  hostfqdn     Success
192.168.1.42  hostfqdn     Success
192.168.1.45  hostfqdn     Success
192.168.1.47  hostfqdn     Success
192.168.1.48  hostfqdn     Success
192.168.1.52  hostfqdn     Success
192.168.1.54  hostfqdn     TimeOut
192.168.1.55  hostfqdn     TimeOut
192.168.1.56  hostfqdn     Success
192.168.1.58  hostfqdn     Success
192.168.1.59  hostfqdn     TimeOut
192.168.1.61  hostfqdn     Success
192.168.1.62  hostfqdn     Success
192.168.1.64  hostfqdn     Success
192.168.1.65  hostfqdn     TimeOut
192.168.1.66  hostfqdn     TimeOut
192.168.1.67  hostfqdn     Success
192.168.1.68  hostfqdn     Success
192.168.1.69  hostfqdn     TimeOut
192.168.1.70  hostfqdn     Success
192.168.1.71  hostfqdn     Success
192.168.1.72  hostfqdn     Success
192.168.1.74  hostfqdn     Success
192.168.1.75  hostfqdn     Success
192.168.1.76  hostfqdn     Success
192.168.1.77  hostfqdn     Success
192.168.1.78  hostfqdn     Success
192.168.1.80  hostfqdn     TimeOut
192.168.1.81  hostfqdn     Success
192.168.1.82  hostfqdn     Success
192.168.1.84  hostfqdn     Success
192.168.1.85  hostfqdn     Success
192.168.1.87  hostfqdn     Success
192.168.1.88  hostfqdn     Success
192.168.1.89  hostfqdn     Success
192.168.1.90  hostfqdn     Success
192.168.1.91  hostfqdn     Success
192.168.1.93  hostfqdn     TimeOut
192.168.1.94  hostfqdn     Success
192.168.1.96  hostfqdn     Success
192.168.1.97  hostfqdn     Success
192.168.1.99  hostfqdn     Success
192.168.1.100 hostfqdn     Success
192.168.1.101 hostfqdn     Success
192.168.1.102 hostfqdn     Success
192.168.1.103 hostfqdn     Success
192.168.1.104 hostfqdn     Success
192.168.1.105 hostfqdn     Success
192.168.1.106 hostfqdn     Success
192.168.1.109 hostfqdn     Success
192.168.1.110 hostfqdn     Success
192.168.1.111 hostfqdn     TimeOut
192.168.1.112 hostfqdn     Success
192.168.1.113 hostfqdn     Success
192.168.1.114 hostfqdn     Success
192.168.1.115 hostfqdn     Success
192.168.1.116 hostfqdn     Success
192.168.1.117 hostfqdn     Success
192.168.1.119 hostfqdn     Success
192.168.1.122 hostfqdn     Success
192.168.1.123 hostfqdn     Success
192.168.1.124 hostfqdn     Success
192.168.1.125 hostfqdn     Success
192.168.1.131 hostfqdn     Success
192.168.1.132 hostfqdn     Success
192.168.1.133 hostfqdn     Success
192.168.1.134 hostfqdn     Success
192.168.1.140 hostfqdn     Success
192.168.1.141 hostfqdn     Success
192.168.1.142 hostfqdn     Success
192.168.1.143 hostfqdn     Success
192.168.1.144 hostfqdn     Success
192.168.1.145 hostfqdn     Success
192.168.1.146 hostfqdn     Success
192.168.1.147 hostfqdn     Success
192.168.1.148 hostfqdn     Success
192.168.1.149 hostfqdn     Success
192.168.1.150 hostfqdn     Success
192.168.1.151 hostfqdn     Success
192.168.1.152 hostfqdn     Success
192.168.1.153 hostfqdn     Success
192.168.1.190 hostfqdn     Success
192.168.1.191 hostfqdn     TimeOut
192.168.1.192 hostfqdn     Success
192.168.1.199 hostfqdn     Success
192.168.1.200 hostfqdn     Success
192.168.1.201 hostfqdn     Success
192.168.1.202 hostfqdn     Success
192.168.1.204 hostfqdn     Success
192.168.1.206 hostfqdn     Success
192.168.1.208 hostfqdn     Success
192.168.1.210 hostfqdn     Success
192.168.1.211 hostfqdn     Success
192.168.1.212 hostfqdn     Success
192.168.1.213 hostfqdn     Success
192.168.1.214 hostfqdn     Success
192.168.1.215 hostfqdn     Success
192.168.1.217 hostfqdn     Success
192.168.1.219 hostfqdn     Success
192.168.1.220 hostfqdn     Success
192.168.1.221 hostfqdn     Success
192.168.1.223 hostfqdn     Success
192.168.1.224 hostfqdn     Success
192.168.1.228 hostfqdn     Success
192.168.1.229 hostfqdn     Success
192.168.1.230 hostfqdn     Success
192.168.1.237 hostfqdn     Success
192.168.1.240 hostfqdn     Success
192.168.1.242 UNKNOWN      Success
192.168.1.245 hostfqdn     Success
192.168.1.248 hostfqdn     Success
192.168.1.249 hostfqdn     Success
192.168.1.253 hostfqdn     Success
192.168.1.254 hostfqdn     Success
1 Like

@riedyw … is Test-Network version dependent or require a module to install? It is not found on my PS 5.1 or PS 7.1.2. Can you elaborate please? Thanks.

It’s in his poshfunctions module he mentioned.

Get-Command -name Test-Network -Module PoshFunctions

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Test-Network                                       2.2.1.6    PoshFunctions

It’s got quite a few functions


Get-Command -Module PoshFunctions |
    Measure-Object | Select-Object -ExpandProperty Count
182

Thanks for the info. I know I had seen a post here in the past that outlined those modules. Thanks for the clarification.

Inspiration for many of the functions were inspired by things I found online. Many were written by me. A large random mix of functions.

I appreciate the info, I am not aware what poshfunctions are. I do apologize.
Also do you have a link to the gallery? As i stated before, I’m very noob to powershell. as in like last week.

The PowerShell Gallery is a software repository of modules and scripts that acts as a place for publishing those scripts and modules. These items can be installed through a PowerShell command.

Install-Module -Name PoshFunctions

You most likely will be prompted the PowerShell Gallery is an untrusted repository. If you run the command Get-PSRepository you will see the repositories that are registered on your machine.

PowerShell Gallery also has a web front end that you can browse. The web address is https://PowerShellGallery.com

To use the commands in the module run the command Import-Module -Name PoshFunctions which will load the module and all its functions into your current PowerShell session. You can see a listing of all the commands in the module by running Get-Command -Module Poshfunctions and there are over 180 different functions in that module.

May I ask if you Googled for “Powershell Gallery”? Or Googling “installing module from Powershell Gallery”? Learning things in the computer field is a journey of discovery. I gave you enough keywords so that you could have searched yourself.

Are you a noob with google as well? :thinking: :wink: It will not hurt you when you try to find something by yourself. :wink: :wink: :wink:

1 Like

Olaf, Id appreciate if you kept the sarcasm and passive aggressive insults to your frozen castle. Thank you.

Thank you for the reply, This is helpful. I now see it.