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
Hi.
nice script.
I made some suggestions.
code is here:
http://poshcode.org/2053
regards
Oliver
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!