Skip to content

New-HostNetworkAdapter

VMware: Creates a new HostVirtualNIC on the specified host

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true)]
    [string]$HostName,
    [Parameter(Mandatory = $true)]
    [string]$SwitchName,
    [Parameter(Mandatory = $true)]
    [string]$PortGroup,
    [bool]$AutomaticIPv6,
    [switch]$Dhcp,
    [bool]$FaultToleranceLoggingEnabled,
    [string]$IPv4,
    [string]$IPv6,
    [bool]$IPv6Enabled,
    [bool]$IPv6ThroughDhcp,
    [string]$MACAddress,
    [bool]$ManagementTrafficEnabled,
    [int32]$MtuSize,
    [string]$SubnetMask,
    [bool]$VMotionEnabled,
    [bool]$VsanTrafficEnabled,
    [switch]$ConsoleNic
)
Process {
    $vmServer = $null
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $vmHost = Get-VMHost -Server $vmServer -Name $HostName -ErrorAction Stop
        $vSwitch = Get-VirtualSwitch -VMHost $vmHost -Name $SwitchName -ErrorAction Stop
        $vAdapter = New-VMHostNetworkAdapter -VMHost $vmHost -VirtualSwitch $vSwitch -ConsoleNic:$ConsoleNic -AutomaticIPv6:$AutomaticIPv6 -PortGroup $PortGroup -IPv6ThroughDhcp:$IPv6ThroughDhcp -Confirm:$false -ErrorAction Stop
        $cmdArgs = @{ ErrorAction = 'Stop'; VirtualNic = $vAdapter; Confirm = $false }
        $output = $null
        if ($PSBoundParameters.ContainsKey('Dhcp')) { $output = Set-VMHostNetworkAdapter @cmdArgs -Dhcp:$Dhcp -Confirm:$false -ErrorAction Stop | Select-Object * }
        if ($PSBoundParameters.ContainsKey('FaultToleranceLoggingEnabled')) { $output = Set-VMHostNetworkAdapter @cmdArgs -FaultToleranceLoggingEnabled $FaultToleranceLoggingEnabled -Confirm:$false -ErrorAction Stop | Select-Object * }
        if ($PSBoundParameters.ContainsKey('IPv4')) { $output = Set-VMHostNetworkAdapter @cmdArgs -IP $IPv4 | Select-Object * }
        if ($PSBoundParameters.ContainsKey('IPv6')) { $output = Set-VMHostNetworkAdapter @cmdArgs -IPv6 $IPv6 | Select-Object * }
        if ($PSBoundParameters.ContainsKey('IPv6ThroughDhcp')) { $output = Set-VMHostNetworkAdapter @cmdArgs -IPv6ThroughDhcp $IPv6ThroughDhcp | Select-Object * }
        if ($PSBoundParameters.ContainsKey('MACAddress')) { $output = Set-VMHostNetworkAdapter @cmdArgs -MAC $MACAddress | Select-Object * }
        if ($PSBoundParameters.ContainsKey('ManagementTrafficEnabled')) { $output = Set-VMHostNetworkAdapter @cmdArgs -ManagementTrafficEnabled $ManagementTrafficEnabled | Select-Object * }
        if ($PSBoundParameters.ContainsKey('MtuSize')) { $output = Set-VMHostNetworkAdapter @cmdArgs -Mtu $MtuSize | Select-Object * }
        if ($PSBoundParameters.ContainsKey('SubnetMask')) { $output = Set-VMHostNetworkAdapter @cmdArgs -SubnetMask $SubnetMask | Select-Object * }
        if ($PSBoundParameters.ContainsKey('VMotionEnabled')) { $output = Set-VMHostNetworkAdapter @cmdArgs -VMotionEnabled $VMotionEnabled | Select-Object * }
        if ($PSBoundParameters.ContainsKey('VsanTrafficEnabled')) { $output = Set-VMHostNetworkAdapter @cmdArgs -VsanTrafficEnabled $VsanTrafficEnabled | Select-Object * }
        if ($PSBoundParameters.ContainsKey('IPv6Enabled')) { $output = Set-VMHostNetworkAdapter @cmdArgs -IPv6Enabled $IPv6Enabled | Select-Object * }
        if ($null -ne $output) {
            foreach ($item in $output) {
                $item | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $timestamp -Force
                Write-Output $item
            }
        }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Name of the host whose network adapter to add

Name of the virtual switch to add the adapter to

Port group for the new adapter

IPv6 address obtained through router advertisement

Off

Use DHCP server

Enable Fault Tolerance logging

IPv4 address

IPv6 address

Enable IPv6 configuration

IPv6 address via DHCP

MAC address of the adapter

Enable for management traffic

MTU size

Subnet mask

Enable for VMotion

Enable Virtual SAN traffic

Off

Create a service console virtual network adapter

An interactive directory of PowerShell scripts.