Public/Find-MacVendor.ps1

function Find-MacVendor {
    <#
    .SYNOPSIS
        Search the registered vendor / IEEE block directory. Requires an API key.

    .DESCRIPTION
        Calls GET /v1/vendors. Filters by organization name substring and/or
        country and returns the matching blocks. The total match count
        (ignoring -Limit) is written to the verbose stream and, with
        -Detailed, returned as a wrapper object instead.

    .PARAMETER Query
        Organization name, or part of one. Case-insensitive.

    .PARAMETER Country
        ISO 3166-1 alpha-2 country code, e.g. US, DE, JP.

    .PARAMETER Limit
        Maximum blocks to return, 1 to 100. The API default is used when omitted.

    .PARAMETER Detailed
        Return a single object with Total and Blocks, rather than streaming
        the blocks.

    .PARAMETER ApiKey
        API key. Falls back to Set-MacadressConfiguration, then the
        MACADRESS_API_KEY environment variable.

    .PARAMETER Raw
        Return the decoded API response verbatim.

    .EXAMPLE
        Find-MacVendor -Query Cisco -Country US -Limit 10

    .EXAMPLE
        Find-MacVendor Apple -Detailed

    .LINK
        https://macadress.com/docs

    .LINK
        https://github.com/sapisos/macadress-powershell
    #>

    [CmdletBinding()]
    [OutputType('Macadress.VendorBlock')]
    param(
        [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('Name', 'Organization')]
        [string] $Query,

        [Parameter()]
        [ValidatePattern('^[A-Za-z]{2}$')]
        [string] $Country,

        [Parameter()]
        [ValidateRange(1, 100)]
        [int] $Limit,

        [Parameter()]
        [switch] $Detailed,

        [Parameter()]
        [string] $ApiKey,

        [Parameter()]
        [switch] $Raw
    )

    process {
        $null = Assert-MacadressApiKey -ApiKey $ApiKey -Operation 'Find-MacVendor'

        $queryParams = @{}
        if ($Query) { $queryParams['query'] = $Query }
        if ($Country) { $queryParams['country'] = $Country }
        if ($PSBoundParameters.ContainsKey('Limit')) { $queryParams['limit'] = $Limit }

        try {
            $response = Invoke-MacadressApi -Method GET -Path 'v1/vendors' -Query $queryParams -ApiKey $ApiKey
        }
        catch {
            Write-Error -ErrorRecord $_
            return
        }

        if ($Raw) { return $response }

        $total = [int] $response.total
        $blocks = @($response.blocks)
        Write-Verbose "Total matches (ignoring limit): $total"

        foreach ($block in $blocks) {
            if ($block -is [psobject] -and -not $block.PSObject.TypeNames.Contains('Macadress.VendorBlock')) {
                $block.PSObject.TypeNames.Insert(0, 'Macadress.VendorBlock')
            }
        }

        if ($Detailed) {
            [pscustomobject]@{
                PSTypeName = 'Macadress.VendorSearchResult'
                Total      = $total
                Count      = $blocks.Count
                Blocks     = $blocks
            }
        }
        else {
            $blocks
        }
    }
}