Retrieve computer status with IP Address

Here is a simple script that I posted both in the Powershell forums and on the technet script repository.  Basically all it does is take either a provided computername or reads from a host list and proceeds to “ping” each computer to test for connectivity and if reachable, also grabs the IP address of the machine and adds to an array.  Machines that are not reachable are added to the list but without the IP address.


<#
.SYNOPSIS
Tests computer for connection and lists status with IP address.
.DESCRIPTION
Tests computer for connection and lists status with IP address.
.PARAMETER server
Name of server to test connection to.
.PARAMETER file
Name of host file to test connection to.
.PARAMETER credential
Allows the use of alternate credentials, if required.
.NOTES
Name: Get-ComputerIP.ps1
Author: Boe Prox
DateCreated: 05Aug2010

.LINK
http://
.EXAMPLE
Get-ComputerIP

#>
[cmdletbinding(
SupportsShouldProcess = $True,
DefaultParameterSetName = ‘computer’,
ConfirmImpact = ‘low’
)]
param(
[Parameter(
Mandatory = $False,
ParameterSetName = ‘computer’,
ValueFromPipeline = $True)]
[string]$computer,
[Parameter(
Mandatory = $False,
ParameterSetName = ‘file’)]
[string]$file,
[Parameter(
Mandatory = $False,
ParameterSetName = ”)]
[switch]$credential
)
If ($file) {
$computers = Get-Content $file
}
Else {
$computers = $computer
}
If ($credential) {
$cred = Get-Credential
}
$report = @()
ForEach ($computer in $Computers) {
Try {
$tempreport = New-Object PSObject
If ($credential) {
$IP = ((Test-Connection -ea stop -Count 1 -comp $computer -credential $cred).IPV4Address).IPAddresstoString
}
Else {
$IP = ((Test-Connection -ea stop -Count 1 -comp $computer).IPV4Address).IPAddresstoString
}
$tempreport | Add-Member NoteProperty Computer $computer
$tempreport | Add-Member NoteProperty Status “Up”
$tempreport | Add-Member NoteProperty IP $ip
$report += $tempreport
}
Catch {
$tempreport = New-Object PSObject
$tempreport | Add-Member NoteProperty Computer $computer
$tempreport | Add-Member NoteProperty Status “Down”
}
}
$report


About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell, scripts and tagged , , . Bookmark the permalink.

2 Responses to Retrieve computer status with IP Address

  1. Oliver Lipkau says:

    Hi.

    nice script.
    I made some suggestions.
    code is here:
    http://poshcode.org/2053

    regards
    Oliver

    • boeprox says:

      Thanks for the suggestions. Always nice to have a second set of eyes looking at my code. I admit I was a little lazy with the SupportsShouldProcess (used a template in the build). Only reason I use a -File param is that it had been requested before where I work at and now it has become a bad habit 🙂 Thanks again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s