Skip to content

Instantly share code, notes, and snippets.

@andymac4182
Last active December 22, 2015 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymac4182/6517253 to your computer and use it in GitHub Desktop.
Save andymac4182/6517253 to your computer and use it in GitHub Desktop.
param (
[string]$range = '10.1.80'
)
# Usage:
# getcompnames.ps1 -range 'x.x.x'
# default range: 10.1.80
$erroractionpreference = "SilentlyContinue"
$Outbook = New-Object -comobject Excel.Application
$Outbook.visible = $True
$Workbook = $Outbook.Workbooks.Add()
$Worksheet = $Workbook.Worksheets.Item(1)
$Worksheet.Cells.Item(1,1) = "IP Address"
$Worksheet.Cells.Item(1,2) = "Remote User"
$Worksheet.Cells.Item(1,3) = "Hostname"
$Formatting = $Worksheet.UsedRange
$Formatting.Interior.ColorIndex = 19
$Formatting.Font.ColorIndex = 11
$Formatting.Font.Bold = $True
$excelRow = 2
for ($i=1; $i -lt 256; $i++) {
$IP = $range +'.' + $i;
$Ping = New-Object System.Net.NetworkInformation.Ping
$Reply = $Ping.send($IP,10)
$Worksheet.Cells.Item($excelRow,1) = $IP.ToUpper()
# If ping was successful
if ($Reply.status -eq "success") {
$Worksheet.Cells.Item($excelRow,2).Interior.ColorIndex = 6
$Worksheet.Cells.Item($excelRow,2) = "Loading..."
# Get an object with details of the computer (e.g. logged on user)
$RemoteSys = Get-WmiObject -Comp $IP -CL Win32_ComputerSystem
# Get the hostname of the IP Address (e.g. TS-00XXX)
$HostName = [System.Net.Dns]::GetHostByAddress($IP).HostName
#if it can get the logged on user
If ($?) {
# Set the field to green and put
$Worksheet.Cells.Item($excelRow,2).Interior.ColorIndex = 4
$Worksheet.Cells.Item($excelRow,2) = $RemoteUser = $RemoteSys.UserName
$Worksheet.Cells.Item($excelRow,3).Interior.ColorIndex = 4
$Worksheet.Cells.Item($excelRow,3) = $HostName
# if it can ping the ip, but no one is logged on then show an error
} Else {
$Worksheet.Cells.Item($excelRow,2).Interior.ColorIndex = 3
$Worksheet.Cells.Item($excelRow,2) = "No Data"
$Worksheet.Cells.Item($excelRow,3).Interior.ColorIndex = 3
$Worksheet.Cells.Item($excelRow,3) = "No Data"
}
} Else {
# no success reply, i.e. unreachable host
$Worksheet.Cells.Item($excelRow,2).Interior.ColorIndex = 3
$Worksheet.Cells.Item($excelRow,2) = "Not Pingable"
}
# Really cool part, auto fit when finished with each line...
$Formatting.EntireColumn.AutoFit()
$Reply = ""
$pwage = ""
$excelRow = $excelRow + 1
}
$Formatting.EntireColumn.AutoFit()
cls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment