private/Get-NetworkAdapter.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
Function Get-NetworkAdapter
{
    <#
        .SYNOPSIS
        Gets the correstponding network adapter for the specified local IP address.
 
        .DESCRIPTION
        Internal Function.
 
        This replaces the Get-NetAdapter call in previous versions, for all intents and purposes this is a decent subsititue.
        The reason why we want to perform this replacement is so that we don't have the dependency on the Get-NetAdapter and NetTCPIP module,
        as they are not available to PowerShell 6 Core.
 
        .OUTPUTS
        System.Net.NetworkInformation.SystemUnicastIPAddressInformation that has informaiton on the network card (actually the address assignment).
    #>


    [CmdletBinding()]
    [OutputType([System.Net.NetworkInformation.IPAddressInformation])]
    Param
    (
        # Socket of the Client
        [Parameter(Mandatory   = $true,
                   HelpMessage = 'Add help message for user')]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$')]
        [String]
        $IPAddress
    )

    # Get a list of network adapters on the system
    $LocalAdapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
    Write-Debug -Message ('Found {0} network interfaces' -f ($LocalAdapters.Count))

    # Get the adapter that the endpoint is assigned to
    $NetworkAdapter = $LocalAdapters.ForEach({$_.GetIPProperties().UnicastAddresses}).where({$_.address -eq $IPAddress})

    if ($NetworkAdapter.Count -ne 1)
    {
        Throw 'Unable to find correct network adapter'
    }

    $NetworkAdapter
}