Monthly Archives: May 2017

Ping computers via Powershell

We have all used ping.exe from a command line at some point for quickly resolving computer host names to an IP address. I created a Powershell script whilst onsite with a client last week to provide the same functionality. Using Active Directory as a data source to obtain all computer accounts which were enabled. Then utilizing the test-computer cmdlet via Powershell to check if the computers were online.

The output is then saved into a nice csv file which can be easily read in Excel.

There are a few freeware tools such as Angry IP scanner or Advanced IP scanner that can do something similar.

Initial code is below and available via GITHUB [davidmcisaac/Powershellpub].

Active Directory powershell module is required to be installed for script to execute.

<#Created by DM 050517 Resolve_ComputerName_2_IP.ps1 #>
$comp =@();$rep =@();$cp =@()
$IPNull = "N/A"
$PingSuccess ="Online"
$PingnonSuccess ="Offline"
$comp = Get-ADComputer -Filter * |Where-Object {$_.enabled -eq $true}|select DNSHostName
foreach ($cp in $comp)
    {#Loop through each computer found
        $t= Test-Connection -ComputerName $cp.DNSHostName -Count 1 -ErrorAction SilentlyContinue -ErrorVariable CatchError
        if ([string]::IsNullOrEmpty($catcherror))
            {#Test Connection received reply. Write IP to csv
            $rep += $t|select @{Name="Computer";exp={[string]$_.Address}},IPv4Address,ipv6address,@{Name="Status";exp={[string]$PingSuccess}}
            {#Write error to csv
            $rep += $cp|select @{Name="Computer";exp={$cp.DNSHostName}},@{Name="IPv4Address";exp={[string]$IPNull}},@{Name="IPv6Address";exp={[string]$IPNull}},@{Name="Status";exp={[string]$PingnonSuccess}}
$rep|export-csv -NoTypeInformation C:\computer_2_ip_report.csv

Plan to make a few changes in the future, to account for larger environment where filtering by Organisation Unit would be useful