Showing posts with label VM. Show all posts
Showing posts with label VM. Show all posts

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.

Monday, October 19, 2009

Hyper-V Automation through scripts (Memory)

Last time I looked at building onto our WMI automation script with processor creation.  Today, I want to cover how to add memory resources into the script.  Once again, I will take advantage of the existing patterns for creating this script.

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

$GuestVM = "Win2k8"

$Namespace = "root\virtualization"

$Computer = "Hyper-V2k8"

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

-ComputerName $Computer

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

Msvm_ComputerSystem Where ElementName='$GuestVM'"


#Associating Msvm_VirtualSystemSettingData class with $VM$VMVSSD = Get-WmiObject -Namespace $Namespace -Query "Associators of {$VM} Where

ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState"


# Define instance of Virtual IDE controller through an association$VMMEM = (Get-WmiObject -Namespace $Namespace -Query "Associators of ($VMVSSD) Where

ResultClass=Msvm_MemorySettingData AssocClass=Msvm_VirtualSystemSettingDataComponent"


| where-object -FilterScript {$_.ResourceSubType -eq "Microsoft Virtual Machine Memory"})

# Define memory resource attributes# set amount of memory (in megabytes)$VMMem.VirtualQuantity = [string]2048# set other attributes that are viewable in Hyper-V manager$VMMem.Reservation = [string]2048$VMMem.Limit = [string]2048

$VSMSvc.ModifyVirtualSystemResources($VM._Path, $VMMem.PSBase.GetText(1))

There you go. Memory has been defined and created for the virtual machine.  Next time, I will add a network resource via WMI.

Friday, October 16, 2009

Hyper-V Automation through scripts (Processor)

Last time I covered automatic Hyper-V through WMI for virtual disks.  Now, I want to cover how to automate the creation of processor resources.  One thing you will notice is that the pattern used to create virtual disks through WMI will be very similar for creating the rest of our resources.  As we figure out how to manipulate settings of virtual machine components, it becomes easier to build a complete script

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

$GuestVM = "Win2k8"

$Namespace = "root\virtualization"

$Computer = "Hyper-V2k8"

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

-ComputerName $Computer

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

Msvm_ComputerSystem Where ElementName='$GuestVM'"


#Associating Msvm_VirtualSystemSettingData class with $VM$VMVSSD = Get-WmiObject -Namespace $Namespace -Query "Associators of {$VM} Where

ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState"


# Define instance of Virtual processor through an association$VMProc = (Get-WmiObject -Namespace $Namespace -Query "Associators of ($VMVSSD) Where

ResultClass=Msvm_ProcessorSettingData AssocClass=Msvm_VirtualSystemSettingDataComponent"


| where-object -FilterScript {$_.ResourceSubType -eq "Microsoft Processor"})

# Define Processor resource attributes# set number of processors$VMProc.VirtualQuantity = [string]1# set other attributes that are viewable in Hyper-V manager$VMProc.Reservation = [string]0$VMProc.Limit = [string]100000$VMProc.Weight = [string]100$VSMSvc.ModifyVirtualSystemResources($VM._Path, $VMProc.PSBase.GetText(1))

There you are. Adding a virtual processor through WMI using our already established pattern for defining resources.  Next time I will cover adding memory resources.