Created
June 30, 2022 07:30
-
-
Save indented-automation/c6bd9874b5747a8f10e59df863041de9 to your computer and use it in GitHub Desktop.
MAC to manufacturer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Update-ManufacturerList { | |
<# | |
.SYNOPSIS | |
Updates the cached manufacturer list maintained by the IEEE. | |
.DESCRIPTION | |
Update-ManufacturerList attempts to download the assigned list of MAC address prefixes using Get-WebContent. | |
The return is converted into an XML format to act as the cache file for Get-Manufacturer. | |
.PARAMETER Source | |
By default, the manufacturer list is downloaded from http://standards.ieee.org/develop/regauth/oui/oui.txt. An alternate source may be specified if required. | |
.INPUTS | |
System.String | |
.EXAMPLE | |
Update-ManufacturerList | |
.NOTES | |
Author: Chris Dent | |
Change log: | |
02/04/2015 - Chris Dent - Refactored. | |
08/05/2013 - Chris Dent - Created. | |
#> | |
[CmdletBinding()] | |
param( | |
[String]$Source = "http://standards-oui.ieee.org/oui.txt" | |
) | |
$Writer = New-Object IO.StreamWriter("$psscriptroot\..\var\oui.xml") | |
$Writer.WriteLine("<?xml version='1.0'?>") | |
$Writer.WriteLine("<Manufacturers>") | |
Get-WebContent $Source -UseSystemProxy | | |
ForEach-Object { | |
switch -regex ($_) { | |
'^\s*([0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2})\s+\(hex\)[\s\t]*(.+)$' { | |
$OUI = $matches[1] | |
$Organisation = $matches[2] | |
break | |
} | |
'^\s*([0-9A-F]{6})\s+\(base 16\)[\s\t]*(.+)$' { | |
$CompanyID = $matches[1] | |
[Array]$Address = $matches[2] | |
break | |
} | |
'^\s+(\S+.+)$' { | |
$Address += $matches[1] | |
break | |
} | |
'^\s*$' { | |
if ($OUI -and $Organisation) { | |
$Writer.WriteLine("<Manufacturer>") | |
$Writer.WriteLine("<OUI>$OUI</OUI>") | |
$Writer.WriteLine("<Organization><![CDATA[$Organisation]]></Organization>") | |
$Writer.WriteLine("<CompanyId>$CompanyID</CompanyId>") | |
$Writer.WriteLine("<Address><![CDATA[$($Address -join ', ')]]></Address>") | |
$Writer.WriteLine("</Manufacturer>") | |
} | |
$OUI = $null; $Organisation = $null; $CompanyID = $null; $Address = $null | |
} | |
} | |
} | |
$Writer.WriteLine("</Manufacturers>") | |
$Writer.Close() | |
} | |
function Get-Manufacturer { | |
<# | |
.SYNOPSIS | |
Get the manufacturer associated with a MAC address. | |
.DESCRIPTION | |
Get-Manufacturer attempts to find a manufacturer for a given MAC address. The list of manufacturers is cached locally in XML format, the function Update-ManufacturerList is used to populate and update the cached list. | |
.PARAMETER MACAddress | |
A partial or full MAC address, with or without delimiters. Accepted delimiters are ., - and :. | |
.INPUTS | |
System.String | |
.OUTPUTS | |
System.Object | |
.EXAMPLE | |
Get-Manufacturer 00:00:00:00:00:01 | |
.EXAMPLE | |
Get-Manufacturer 000000000001 | |
.EXAMPLE | |
Get-Manufacturer 00-00-00 | |
.NOTES | |
Author: Chris Dent | |
Change log: | |
02/04/2015 - Chris Dent - Updated to use Indented.Common XPath CmdLets. | |
08/05/2013 - Chris Dent - Created. | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeLine = $true, ValueFromPipelineByPropertyname = $true)] | |
[ValidatePattern('^([0-9A-Z]{2}[.\-:]?){2,5}([0-9A-Z]{2})')] | |
[String]$MACAddress | |
) | |
process { | |
$MACAddress -match '([0-9A-Z]{2})[.\-:]?([0-9A-Z]{2})[.\-:]?([0-9A-Z]{2})' | Out-Null | |
$OUI = [String]::Format("{0}-{1}-{2}", $matches[1], $matches[2], $matches[3]).ToUpper() | |
$FilePath = "$psscriptroot\..\var\oui.xml" | |
if (Test-Path $FilePath) { | |
$XPathNavigator = New-XPathNavigator $FilePath | |
$XPathNavigator.Select("/Manufacturers/Manufacturer[OUI='$OUI']") | | |
ForEach-Object { | |
$_ | ConvertFrom-XPathNode -ToObject | |
} | |
} | |
else { | |
Write-Warning "Get-Manufacturer: The manufacturer list does not exist. Run Update-ManufacturerList to create." | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment