ConfigureVMNetworkAdapter.ps1

Applies To: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

You can configure a virtual machine to use one or more virtual network adapters that connect the virtual machine to internal networks or to external networks after the virtual machine is deployed on a host. To configure a network adapter for a virtual machine, use the Set-VirtualNetworkAdapter cmdlet.

The following script connects the virtual network adapter to a virtual network, changes the MAC address from static to dynamic, and enables virtual LAN (VLAN) identification for the network adapter.

Disclaimer

# Filename:      ConfigureVMNetworkAdapter.ps1
# Description:   Configures a network adapter for a virtual machine.

# Connect to the VMM server.
# Replace \"VMMServer01.Contoso.com\" with the name of your VMM server.
Get-VMMServer -ComputerName "VMMServer01.Contoso.com"

# Get the virtual machine.
# Replace \"VM01\" with the name of your virtual machine.
$VM = Get-VM -name "VM01"

# Get the network adapter. Replace the physical address placeholder
# with the MAC address of your virtual machine.
$Adapter = Get-VirtualNetworkAdapter -VM $VM | where {$_.PhysicalAddress -eq "00:00:00:00:00:00"}

$VMStatus = $VM.status

# Ensure that the virtual machine is stopped.
If ($VMStatus -eq "running")
{
Stop-VM $VM
}

# Connect the virtual network adapter to a virtual network,
# change the MAC address from static to dynamic, and then 
# enable VLAN identification.
# Replace \"Virtual Network\" and \"Corp.Contoso.com\" with the 
# name of your virtual network and the network location.

Set-VirtualNetworkAdapter -VirtualNetworkAdapter $Adapter -VirtualNetwork "Virtual Network" -NetworkLocation "Corp.Contoso.com" -PhysicalAddressType "Dynamic" -VLANEnabled $TRUE -VLANID "2"

# If the virtual machine was running before the update, 
# start it again.
If ($VMStatus -eq "running")
{
Start-VM $VM
}