Resolve-MacAddress.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

<#PSScriptInfo
 
.VERSION 1.16.2
 
.GUID 1d517baf-fecc-4167-8f93-4dbe965ac0fd
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS
 saw-friendship Hardware Vendor MAC MacAddress LinkLayerAddress PhysicalAddress LookUp IEEE
 
.LICENSEURI
 
.PROJECTURI https://sawfriendship.wordpress.com/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
 
 Get Hardware Vendor by MacAddress. Download Database from IEEE site
 
#>
 

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][alias("LinkLayerAddress","PhysicalAddress")][string[]]$MAC,
    [switch]$PassThru,
    [switch]$UpdateFile
)

    if(!$PSScriptRoot){$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent}
    
    $MacVendorTableCSVpath = Join-Path -Path $PSScriptRoot -ChildPath "MacVendorTable.ieee.org.csv"
    $regexEscape = [regex]::Escape('(hex)')
    # $OuiTxtUrl = "http://www.ieee.org/netstorage/standards/oui.txt"
    $OuiTxtUrl = "http://standards-oui.ieee.org/oui.txt"
    if(!(Test-Path Variable:MacVendorTable)){
        $Global:MacVendorTable = @{}
    }
    if($UpdateFile -or (!(Test-Path $MacVendorTableCSVpath))){
        try{
            $ouitxt = Invoke-WebRequest -Uri $OuiTxtUrl -UseBasicParsing
            $oui = $ouitxt.Content -split "`n" | Select-String $regexEscape
        } catch {
            Write-Warning "file not exists"
            break
        }
        $oui | % {
            $MacAddr,$Vendor = $_ -split $regexEscape -replace "^\s+|\s+$"
            $MacAddr = $MacAddr -replace "\-"
            try{
                $MacVendorTable.Add($MacAddr,$Vendor)
            } catch{
            
            }
        }
        $MacVendorTable.GetEnumerator() | sort Name | % {
                [PsCustomObject][Ordered]@{
                    'Mac' = $_.Key
                    'Vendor' = $_.Value
                }
            } | Export-Csv -Delimiter ';' -NoTypeInformation -Path $MacVendorTableCSVpath
    } elseif((Test-Path $MacVendorTableCSVpath) -and ($MacVendorTable.count -eq 0)){
        $Global:MacVendorTable = @{}
        Import-Csv -Delimiter ';' -Path $MacVendorTableCSVpath | % {($Global:MacVendorTable).Add($_.Mac,$_.Vendor)}
    }else{}
    
    $MAC | % {
        $MAC6 = $_ -replace "[g-z]|[^\w\d]" -replace "([\w\d]{6})(.+)",'$1'
        if(($MAC6 -match "\d|\w") -and ($MAC6.Length -eq 6) -and ($MAC6 -ne '000000')){
            if(!$PassThru){
                $MacVendorTable[$MAC6]
            } else {
                [pscustomobject][Ordered]@{
                'MAC' = $_
                'Vendor' = $MacVendorTable[$MAC6]
                }
            }
        }
    }