Trying to locate computers on the network

Frequent Visitor

Good day, I am new to PowerShell but I am trying to create a script (using this one that I have found) that will locate IP and DNS names of machines located on my network. I have tried this but I have a few errors on the script itself. I was hoping if someone could assist or perhaps guide me on this. The part that I get errors, is around the area that is "bold".





#replace the string with e.g. "192.168.1.$_", whatever your subnet is, optional but will improve # of devices found
$ips= 0..255 | ForEach-Object{"192.168.1.$_"};

#optional: add ports to scan. 22=ssh, 80=http, 443=https, 135=smb, 3389=rdp
$ports= 22, 80, 443, 135, 3389;

#optional: change batch size to speed up / slow down (warning: too high will throw errors)

$ips += Get-NetNeighbor | ForEach-Object{$_.IPAddress}
$ips = $ips | Sort-Object | Get-Unique;
$ips | ForEach-Object -Parallel {$batchSize} -ThrottleLimit 5
$activePorts = $using:ports | ForEach-Object{ if(Test-Connection $ip -quiet -TcpPort $_ -TimeoutSeconds 1){ $_ } }
if(Test-Connection $ip -quiet -TimeoutSeconds 1 -count 1){
$dns=(Resolve-DnsName $ip -ErrorAction SilentlyContinue).NameHost;
$mac=((Get-NetNeighbor |Where-Object{$_.State -ne "Incomplete" -and $_.State -ne "Unreachable" -and $_.IPAddress -match $ip}|ForEach-Object{$_}).LinkLayerAddress )
return [pscustomobject]@{dns=$dns; ip=$ip; ports=$activePorts; mac=$mac}
} | Tee-Object -variable "dvcResults"
$dvcResults | Sort-Object -property mac

1 Reply

@MartyDonIt generally helps to have the error message. They do give valuable information.


At a guess, maybe the bold lins should be:

$ips | ForEach-Object -Parallel -ThrottleLimit $batchSize