Skip to content

Instantly share code, notes, and snippets.

@Slusho
Last active January 3, 2018 21:10
Show Gist options
  • Save Slusho/a91dd45e97e741de63b628386efcd750 to your computer and use it in GitHub Desktop.
Save Slusho/a91dd45e97e741de63b628386efcd750 to your computer and use it in GitHub Desktop.
Scans subnet IP range for computers and displays WMI information for each.
# Pings all ipaddresses in given subnet ip range
# Neatly shows IP address, hostname, manufacturer, and model according to WMI
Write-Host 'Enter desired subnet without last octet. Example xxx.xxx.xxx.'
$subNet = Read-Host
$ipFrom = Read-Host 'Last octet range FROM '
$ipTo = Read-Host 'Last octet range TO '
function testRange ($from, $to){
if($from -gt $to){
Write-Host 'Invalid IP range!'-ForegroundColor Red
exit
}
else{
$diff = $to - $from
}
$ipArray = @()
for($i = [Int32]$from; $i -le $diff + [Int32]$from; $i++){
$ipArray += "$subNet$i"
}
$ipArray | ForEach-Object{
$testConn = Test-Connection -ComputerName $_ -BufferSize 16 -Count 2 -ErrorAction 0
if(!($testConn)){
$param = @{
IP = $_
Name = 'IP not in use'
}
New-Object -TypeName psobject -Property $param | ft -AutoSize
}
else{
$obj = Get-WmiObject -ComputerName $_ -Class Win32_ComputerSystem
$param = @{
IP = $_
Name = $obj.Name
Manufacturer = $obj.Manufacturer
Model = $obj.Model
}
New-Object -TypeName psobject -Property $param | ft -AutoSize
}
}
}
testRange $ipFrom $ipTo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment