Producing a list of working servers in domain...

Hi all,

I am trying to write a simple script which reads all AD server objects using get-adcomputer and then using the retreived ipv4address property, ping each of these in turn after, using if/else to seperate the results into resolvable or not .csv files.

The main issue I am experiencing is that the line of code to ping the hostname below will work from the command line:

[pre]

if (test-Connection -ComputerName $Server.ipv4address -Count 1 -Quiet ) {

[/pre]

 

However when run within the script I receive the error:

Test-Connection : Cannot validate argument on parameter ‘ComputerName’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Can anyone tell me what I am doing wrong here?

Full script below:

[pre]

import-module activedirectory

$WorkingServer = @()

$FailedServer = @()

$AllServers = Get-ADComputer -Filter -Properties | select name,operatingsystem,dnshostname,ipv4address

foreach ($Server in $AllServers){

if (test-Connection -ComputerName $Server.ipv4address -Count 1 -Quiet ) {

$WorkingServer += $Server

}

else{

$FailedServer += $Server

}

}

$WorkingServer | export-csv ServersToPatch.csv -NoTypeInformation

$FailedServer | Export-Csv FailedToPingServers.csv -NoTypeInformation

[/pre]

 

Many thanks in advance!

 

Just to add, I am using Powershell 5.1

Why don’t you “ping” the names? The property IPv4Address is actually not an AD attribute it will be populated by the cmdlet under the hood. But if the computer/IPv4Address is not resolveable it will fail - therefore your query fails as well … try to output the list of computers from Get-ADComputer and show only the name and the IPv4Address … then you might see what I mean :wink:

 

Also you are not filtering on server operating systems and do you really need all of the properties back -Properties * as on a large domain this can put unnecessary strain on your DC’s

$AllServers = Get-ADComputer -Filter { OperatingSystem -Like ‘Windows Server’} -Properties name,operatingsystem,dnshostname,ipv4address | select name,operatingsystem,dnshostname,ipv4address

excellent, using name rather than ipv4 address worked fine!

Thanks Olaf, however what if I need to ping fqdn which ipv4 address gives me rather than just the name object?

Thanks SimonB for helping me tidy up my query.

You have the talent to confuse me. :wink: :smiley: … fqdn = Full Qualified Domain Name … not Full Qualified Domain IPv4Address. :wink:

If you need to ping the ip address you can do this. But to get the ip address from the AD with the cmdlet Get-ADUser is not a reliable way - that’s what you should have in mind.

I think it will be more obvious what I mean when you run this code snippet:

Get-ADComputer -Filter * -Properties ipv4address |
Select-Object -Property name, ipv4address