1

I am looking to run a PowerShell script that will pull all servers from AD, show me the FQDN and IP address as well as tell me if the server is pinging.

I have a couple of scripts that do certain parts but would like to get something all in one and am having a hard time doing it.

Ping a list of servers:

$ServerName = Get-Content "c:\temp\servers.txt"
foreach ($Server in $ServerName) {
    if (test-Connection -ComputerName $Server -Count 2 -Quiet ) {
        "$Server is Pinging "
    } else {
        "$Server not pinging"
    }
}

I also have a script to pull all servers from AD which shows server name, FQDN, and OS version:

Import-Module ActiveDirectory
Get-ADComputer -Filter {OperatingSystem -like '*Windows Server*'} -Properties * |
    select Name, DNSHostName, OperatingSystem

Any help in getting a script to show me all servers in my environment with FQDN, IP address and if there live would be appreciated.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
aztech
  • 71
  • 2
  • 5
  • 9
  • 2
    `select Name, DNSHostName, OperatingSystem` -> `select Name, DNSHostName, OperatingSystem, @{n='Online';e={Test-Connection $_.DNSHostName -Count 2 -Quiet}}` – Ansgar Wiechers Dec 13 '18 at 23:22
  • 1
    Beware that depending on the number of servers in your environment doing a sequential sweep will take quite some time. You may want to look into background jobs for running things in parallel. – Ansgar Wiechers Dec 13 '18 at 23:26
  • see my answer in https://stackoverflow.com/questions/53618904/how-to-use-multithreading-script/53620609#53620609 for one approach to getting ping data. how you pull names from your AD is another exercise. putting the two together to suit your needs may be enough for what you are after. Good luck, and welome to Stack Overflow. – Kory Gill Dec 14 '18 at 00:01
  • To make your query for the computer accounts even more efficient, don't do `-Properties *` in `Get-ADComputer`. The only property you need that's not returned by the default Get-AdComputer cmdlet is OperatingSystem, so just return that one extra attribute in your query. `Get-AdComputer -filter {...} -Properties OperatingSystem`. Also consider using `-SearchScope` if all your servers are in one OU. – LeeM Dec 14 '18 at 00:50

1 Answers1

1

You should only choose desired properties with the -Property argument and not pull everything through network with *. Also quotes should be used with -Filter, not braces.

You can add a calculated property to Select-Object and get the value from Test-NetConnection:

Get-ADComputer -Filter "OperatingSystem -like '*Windows Server*'" -Properties dnshostname, operatingsystem |
    Select-Object name, dnshostname, operatingsystem, `
    @{n="PingSucceeded";e={(Test-NetConnection $_.name).PingSucceeded}}
Janne Tuukkanen
  • 1,620
  • 9
  • 13