Thursday, October 22, 2009

Hyper-V Automation through scripts (Network)

Last time I looked at scripting memory resource creating.  Today, I want to look at the final individual component, network resources.  These network resources are virtual network interfaces that are connected to a virtual switch.  A virtual network interface can be connected to a virtual machine, but will not become active until it is attached to a virtual switch.

Let’s dig into the code to perform this network provisioning.

# Set up variables$VHD = "f:\VHDs\win2k8.vhd"

$GuestVM = "Win2k8"

$Namespace = "root\virtualization"

$Computer = "Hyper-V2k8"

# I am assuming the virtual switch and port already exist# created through Hyper-V Manager or through another script$VMSwitchName = "Hyper-V External Switch"$VMSwitchPortName = "VMPort"# Hyper-V uses GUIDs to identify components. Friendly names are only a benefit for admins$VMNICGUID1 = [GUID]::NewGUID().ToString()$VMNICGUID2 = [GUID]::NewGUID().ToString()

# Get instance of the default network interface$DefaultNet = Get-WmiObject -Namespace $Namespace -Class Msvm_SyntheticEthernetPortSettingData | where-object -FilterScript {$_.InstanceID -like "*Default*"}

# Get instance of Msvm_ComputerSystem class$VM = Get-WmiObject -Namespace $Namespace -ComputerName $Computer -Query "Select * From

Msvm_ComputerSystem Where ElementName='$GuestVM'"


# Get instance of Msvm_VirtualSwitchManagementService class$VMSwitchQuery = Get-WmiObject -Class "Msvm_VirtualSwitchManagementService" -Namespace $Namespace

# Get instance of Msvm_VirtualSystemManagementService class$VSMSvc = Get-WmiObject -Class "Msvm_VirtualSystemManagementService" -Namespace $Namespace

-ComputerName $Computer

# Get instance of target virtual switch$VMSwitch = Get-WmiObject -Namespace $Namespace -Query "Select * From Msvm_VirtualSwitch Where ElementName = '$VMSwitchName'

# Create the switch ports$ReturnObject = $VMSwitchQuery.CreateSwitchPort ($VMSwitch, [guid]::NewGuid().ToString(), $VMSwitchPortName, "
")$NewSwitchPort1 = $ReturnObject.CreatedSwitchPort$ReturnObject = $VMSwitchQuery.CreateSwitchPort ($VMSwitch, [guid]::NewGuid().ToString(), $VMSwitchPortName, "")$NewSwitchPort2 = $ReturnObject.CreatedSwitchPort

#Set up the virtual interfaces# I am showing two interfaces, a static and a dynamic addressed model$StatNet = $DefaultNet.psbase.Clone()$StatNet.VirtualSystemIdentifiers = "
{VMNICGUID1}"$StatNet.StaticMacAddress = $true$StatNet.Address = "00155d9290ff"$StatNet.Connection = $NewSwitchPort1

$DynNet = $DefaultNet.psbase.Clone()$DynNet.VirtualSystemIdentifiers = "
{VMNICGUID2}"$DynNet.Connection = $NewSwitchPort2

#set properties and target virtual machine$VSMSvc.AddVirtualSystemResources ($VM._Path, $StatNet.PSBase.GetText(1))$VSMSvc.AddVirtualSystemResources ($VM._Path, $DynNet.PsBase.GetText(1))

That creates our network resources.  Next time, I will take everything and put together a single script that builds a complete virtual machine.

3 comments:

Tweets that mention Hyper-V Automation through scripts (Network) – davidramthun.com -- Topsy.com said...

[...] This post was mentioned on Twitter by VM Digest, David Ramthun. David Ramthun said: Hyper-V Automation through scripts (Network) http://bit.ly/1lpMRX [...]

uberVU - social comments said...

Social comments and analytics for this post...

This post was mentioned on Twitter by dramthun: Hyper-V Automation through scripts (Network) http://bit.ly/1lpMRX...

Hyper-V Automation through scripts (Final Script) – davidramthun.com said...

[...] so far we have created, modified, and added resources (memory, processor, disks, and network) to virtual machines.  Now, we want to take that information and build a complete virtual [...]