Public/Resolve-MacAddress.ps1
|
function Resolve-MacAddress { <# .SYNOPSIS Full analysis of one or more MAC addresses. Requires an API key. .DESCRIPTION For a single address, calls GET /v1/mac/:mac. For more than one (passed as an array or down the pipeline), batches them through POST /v1/mac/batch in groups of 100 and streams the results back in input order. Each result carries the organization, OUI, matched IEEE block and its type, country, vendor address, transmission and administration type, EUI-64 and IPv6 link-local derivations, randomization confidence, a device guess, registry metadata and the database version. A batch entry that failed carries a non-null Error instead. .PARAMETER MacAddress One or more MAC addresses in any common notation. .PARAMETER ApiKey API key. Falls back to Set-MacadressConfiguration, then the MACADRESS_API_KEY environment variable. .PARAMETER Raw Return the decoded API response verbatim (for a batch, the { count, results } envelope) instead of tagged result objects. .EXAMPLE Resolve-MacAddress 3C:22:FB:12:34:56 | Format-List .EXAMPLE Get-Content macs.txt | Resolve-MacAddress | Where-Object { $_.country -eq 'US' } .EXAMPLE (Resolve-MacAddress 00:03:93:AB:12:34).eui64 .LINK https://macadress.com/docs .LINK https://github.com/sapisos/macadress-powershell #> [CmdletBinding()] [OutputType('Macadress.MacResult')] [Alias('Get-MacAddressInfo')] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [Alias('Mac', 'Address', 'PhysicalAddress')] [string[]] $MacAddress, [Parameter()] [string] $ApiKey, [Parameter()] [switch] $Raw ) begin { $null = Assert-MacadressApiKey -ApiKey $ApiKey -Operation 'Resolve-MacAddress' $collected = [System.Collections.Generic.List[string]]::new() } process { foreach ($mac in $MacAddress) { $trimmed = $mac.Trim() if ($trimmed) { $collected.Add($trimmed) } } } end { if ($collected.Count -eq 0) { return } if ($collected.Count -eq 1) { $one = $collected[0] try { $result = Invoke-MacadressApi -Method GET -Path "v1/mac/$(ConvertTo-MacadressPathSegment $one)" -ApiKey $ApiKey } catch { Write-Error -ErrorRecord $_ return } if ($Raw) { return $result } Write-MacadressResult -Result $result -InputAddress $one return } $size = 100 for ($i = 0; $i -lt $collected.Count; $i += $size) { $chunk = @($collected[$i..([math]::Min($i + $size - 1, $collected.Count - 1))]) try { $response = Invoke-MacadressApi -Method POST -Path 'v1/mac/batch' -Body @{ macs = $chunk } -ApiKey $ApiKey } catch { Write-Error -ErrorRecord $_ continue } if ($Raw) { $response; continue } foreach ($item in @($response.results)) { Write-MacadressResult -Result $item -InputAddress $item.input } } } } |