(I am not an IT newbie, but kind of a PowerShell newbie, I have written some scripts but usually get stuck and have to borrow from the scripts and ideas of others. )
Every time I have a specific need for something I am trying to do it in PowerShell to learn it and also to have a repository of snippets for the next time and the next customer. For instance, I recently needed the most recent activity in a directory structure:
Get-ChildItem "C:\Users\JDoe\OneDrive - Contoso" -Recurse `
| sort lastwritetime -Descending `
| select -first 40 lastwritetime, length, name, directory `
| Format-Table -AutoSize
So in this instance I was trying to find a device on the network. It was the only device on the network that was manufactured by a specific vendor so it should've been easy, if I had a script it would've been fast and easy. (because it was a small network I found the device manually, but for the next time...!)
I started by getting all the MAC addresses on the /24 network by using 'arp -a' but thought it would be easier to use the data by doing it in PowerShell. I learned that Get-Neighbor is what I need but it pulls all IP's so I limited it by IPV4; it pulls unreachables and multicasts so I limited it by removing those; I wanted the list by MAC so I sorted it. Here's what I have so far:
Get-NetNeighbor -AddressFamily IPv4 `
| where {$_.State -ne 'Unreachable' -and $_.State -ne 'Permanent'} `
| sort LinkLayerAddress -Unique `
| Format-Table -AutoSize IPAddress, LinkLayerAddress, State
IPAddress LinkLayerAddress State
--------- ---------------- -----
192.168.123.34 08e689e28251 Reachable
192.168.123.24 0e1b2f9f1cea Stale
etc.
Sidebar: why does this not work?
| Where-Object {$_.State -ne ('Permanent' -and 'Unreachable')} `
What I want to do from here is to sort uniquely the first 6 characters of the MAC address to get all unique vendor IP ranges, then call the macvendors site for each vendor range, something like this in PS V7:
Invoke-WebRequest -Uri "https://api.macvendors.com/"$_.LinkLayerAddress""
If I surf to
https://api.macvendors.com/08-BD-43
I get: NETGEAR
If I call it from PowerShell I get this:
Invoke-WebRequest -Uri "https://api.macvendors.com/08-BD-43"
StatusCode : 200
StatusDescription : OK
Content : NETGEAR
RawContent : HTTP/1.1 200 OK
Cache-Control: must-revalidate, max-age=0, private
Date: Mon, 01 Feb 2021 04:00:21 GMT
Server: Cowboy
X-Request-ID: Fl-DuOn7vVssClx3sObi
Content-Length: 7
Content-Type: text/plai…
Headers : {[Cache-Control, System.String[]], [Date, System.String[]], [Server, System.String[]],
[X-Request-ID, System.String[]]…}
Images : {}
InputFields : {}
Links : {}
RawContentLength : 7
RelationLink : {}
What I would like to end up with is this:
IPAddress OUI Vendor
--------- ------ -----
192.168.123.34 08e689 Apple, Inc.
192.168.123.24 08bd43 NETGEAR
etc.
This is typically my frustration. I know what I want to do, I can find the commands I need to do it, but I can't put it together. I would be greatly appreciative of anyone who could point me in a direction. -Rob