Skip to content

Get-HyperVVMVLAN

Hyper-V: Audits virtual machine VLAN settings

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

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

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name,

    [string]$AdapterName
)

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

        $vm = Get-VM @params | Where-Object { $_.Name -eq $Name -or $_.Id -eq $Name }

        if (-not $vm) {
            throw "Virtual machine '$Name' not found on '$ComputerName'."
        }

        $vlanParams = @{ 'VM' = $vm; 'ErrorAction' = 'Stop' }
        if ($AdapterName) {
            $vlanParams.Add('VMNetworkAdapterName', $AdapterName)
        }

        $results = Get-VMNetworkAdapterVlan @vlanParams | Select-Object @{N='VMName';E={$vm.Name}}, VMNetworkAdapterName, OperationMode, AccessVlanId, CommunityVlanId, IsolatedVlanId, PrimaryVlanId

        Write-Output $results
    }
    catch {
        throw
    }
}

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

Specifies the credentials to use for the remote connection.

Specifies the name or ID of the virtual machine.

Optional. Specifies the name of the network adapter. If not provided, all adapters are audited.

An interactive directory of PowerShell scripts.