Skip to content

Get-HyperVHostNetworkAdapter

Hyper-V: Audits host management network adapters

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [string]$Name = "*",

    [switch]$IncludeVlan
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ManagementOS' = $true
            'Name'         = $Name
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $adapters = Get-VMNetworkAdapter @params
        
        $results = foreach ($ada in $adapters) {
            $record = [PSCustomObject]@{
                ComputerName = $ada.ComputerName
                AdapterName  = $ada.Name
                SwitchName   = $ada.SwitchName
                MacAddress   = $ada.MacAddress
                Status       = $ada.Status
                IsManagement = $ada.IsManagementOs
            }
            if ($IncludeVlan) {
                $vlan = Get-VMNetworkAdapterVlan -VMNetworkAdapter $ada
                $record | Add-Member -MemberType NoteProperty -Name "VlanMode" -Value $vlan.OperationMode
                $record | Add-Member -MemberType NoteProperty -Name "VlanId" -Value $vlan.AccessVlanId
            }
            $record
        }

        Write-Output ($results | Sort-Object AdapterName)
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Optional. Specifies the name of a specific management network adapter.

Off

Optional. If set, includes detailed VLAN configuration for the adapters.

An interactive directory of PowerShell scripts.