PowerShell script to check if computer is online
2016-OCT-26

I wrote this PowerShell function a while back. I might have a better/newer version of this somewhere, if I find it, I'll update this post with it. With that said, this requires the Quest ActiveRoles cmdlets. I don't like what Dell is doing with the Quest cmdlets, but at least you can find version 1.51 download still for free.

<#
    .SYNOPSIS
        Take an IP address or a computer name as input and test to see if that
        computer is valid, and if it's pingable

    .DESCRIPTION
        Takes an input and does some validation on the input

    .EXAMPLE
        Using with an IP address
        Check-Computer 192.168.1.1

    .EXAMPLE
        Using with a computer name
        Check-Computer ott-0001

    .NOTES
        Stephen Lam
#>
function Check-Computer ($ipOrCompName)
{
    #//Add Quest CMDlets as it's needed for this script
    Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue

    if($ipOrCompName -ne $null)
    {
        #//Test to see if the $ipOrCompName is a valid IP format
        #//IP is in a valid IP format; it will use Test-Connection CMDlet to see
        #//if that IP is pingable
        if([bool]($ipOrCompName -as [Net.IPAddress])){
            if(Test-Connection -ComputerName $ipOrCompName -Count 1 -Quiet -ErrorAction SilentlyContinue){
                $compName = [System.Net.Dns]::GetHostEntry($ipOrCompName).HostName
            }
            else{
                #//Couldn't find a computer that's base on the IP address
                $compName = $null
            }
        }

        #//Since the input is not in a valid IP format, it will test to see if this is or
        #//a part of a computer name
        else{
            #//Searches the whole AD for computer that starts with $ipOrCompName
            $compTest = Get-QADComputer -Identity $ipOrCompName*

            #//Checks to see if there's more than one computer object that returns from the search
            #//If there is, it will either list all the computer it found and allow the user to
            #//select the computer they want, or if it's just one computer that returns from the
            #//search, it will use that one automatically
            if ($compTest -ne $null){
                #//More than one computer object found with that search criteria
                If($compTest.count -gt 1){
                    $counter = 1
                    Write-Host "Found" $compTest.Count "that matches" $ipOrCompName
                    foreach($obj in $compTest){
                        Write-Host "$counter)" $obj.Name
                        $counter++
                    }
                    $select = Read-Host "Please enter the your selection (eg. 1 for" $compTest[0].Name")"

                    #//Checks to see if the selected computer is pingable/online
                    if(Test-Connection -ComputerName $compTest[$select-1].Name -Count 1 -Quiet){
                        $compName = $compTest[$select-1].Name
                    }
                    else{
                        #//Couldn't ping the computer
                        $compName = $null
                    }
                }

                #//Only one computer object found in the AD that matches the criteria
                else{
                    #//Checks to see if the selected computer is pingable/online
                    if(Test-Connection -ComputerName $compTest.Name -Count 1 -Quiet -ErrorAction SilentlyContinue){
                        $compName = $compTest.Name
                    }
                    else{
                        $compName = $null
                    }
                }
            }

            else{
                $compName = $null
            }
        }
    }
    else{
        #//No computer found base on the IPorCompName
        $compName = $null
    }
    #//Returns the $compName either with a name or $null if it did not find anything
    return $compName
}