Get-ADComputer along with ipv4address from Test-Connection

I have a list of ADComputers

$computers = Get-ADComputer -Filter {Name -like "Foo*"}

I want to see the subset of those that are on-network and returning pings right now.

$results = @()
foreach ($computer in $computers) { if (Test-Connection -ComputerName $computer.Name -Count 1 -Quiet) { $results += $computer }
}

however, now I find I want both the ADComputer object and the IPv4Address that came back from TestConnection, so I thought I'd filter again and wrap the Computer in a custom hashtable along with the returned IPv4Address.

$results2 = @()
foreach ($result in $results) { $ipv4 = Test-Connection -ComputerName $result.Name -Count 1 | Select -ExpandProperty IPv4Address $results2 += @{Computer=$result; IPv4Address=$ipv4}
}

and then get my results with

$results2 | Format-Table Computer.Name, IPv4Address

However my results are blank

Computer.Name IPv4Address
------------- -----------
(many blank rows follow)

How could I get the results I expect out of this? Or alternatively: am I barking up the wrong tree and there's an easier way to do this?

3

3 Answers

$results = foreach ($c in $computers)
{ $ipv4 = Test-Connection -ComputerName $c.name -count 1 | select -expand IPV4Address [PSCustomObject]@{ "Computer" = $c.name ; "IPV4" = $($ipv4.IPAddressToString) }
}

you got these Empty lines because the IPV4Address Property contains multiple properties instead of one string. the IPAddressToString Property contains the IPv4Address on it's own

your output will be saved in $results if you want them to output directly, just remove the variable in front of foreach

I used a PSCustomObject for this because I like to name my properties, if you want to change that to a hashtable, change the last code line to:

[hashtable]@{ $c = $($ipv4.IPAddressToString) }

Try this...

Clear-Host
(Get-ADComputer -Filter *).Name | %{
If (Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue)
{ Test-Connection -ComputerName $_ -Count 1 ` | Select Address,ProtocolAddress, @{Name = 'Status';Expression = {'Online'}}
}
Else{$_ | Select @{Name = 'Address';Expression = {$_}},
@{Name = 'ProtocolAddress';Expression = {'NoAddress'}},
@{Name = 'Status';Expression = {'Offline'}}
}} | Format-Table -AutoSize

Results

Address ProtocolAddress Status
------- --------------- ------
DC01 192.168.0.11 Online
EX01 192.168.2.12 Online
...
WS01 NoAddress Offline
IIS01 192.168.7.11 Online
... 

Or here is another way to do what you are after

Clear-Host
(Get-ADComputer -Filter *).Name | %{
If (Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue)
{Resolve-DnsName -Name $_ -Type A `
| Select Name,IPaddress,@{Name = 'Status';Expression = {'Online'}}}
Else{
Resolve-DnsName -Name $_ -Type A -ErrorAction SilentlyContinue `
| Select Name,IPaddress,@{Name = 'Status';Expression = {'Offline'}}
}}

Results

Name IPAddress Status
---- --------- ------
DC01.contoso.com 192.168.0.11 Online
EX01.contoso.com 192.168.2.12 Online
...
WS01.contoso.com 192.168.8.30 Offline
IIS01.contoso.com 192.168.7.11 Online
...

I have a PowerShell script for this exact task:

$computers = Get-Content "ListOfWrkStns_Jan18.txt"
foreach ($computer in $computers) { if (test-Connection -ComputerName $computer -Count 2 -Quiet ) { "$computer is up." } else {"$computer is DOWN!" }
} 

You'll just need to create a "ListOfWrkStns.txt" (list of workstations) file to feed it the input of all machine names.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like