Developing Solutions For Windows Server 2008 Hyper-V Using WMI Nihar Shah Senior Development Lead Microsoft Corporation ES1 0
Developing Solutions For Windows Server 2008 Hyper-V Using WMI
Nihar ShahSenior Development LeadMicrosoft Corporation
ES10
Overview Virtual Machines Virtual Machine Management Use Case – Creating a Virtual Machine Virtual Devices Resource Pools Use Case – Attaching a VHD to a Virtual Machine Networking Use Case – Configuring Networking Q & A
Agenda
To provide a detailed overview of the WMI object model in Windows Server 2008 Hyper-V
To communicate “best practices” when programming against the Hyper-V WMI API
To develop an understanding of how to perform common tasksusing Windows Server 2008 Hyper-V
Goals
Version 2.12e of the DMTF standard Concrete subclasses use the Msvm_ prefix
Windows Server 2008 Hyper-V
Version 2.12e of the DMTF standard Concrete subclasses use the Msvm_ prefix
Snapshots Point-in-time representation of a VM
Configuration Disk Runtime state
Windows Server 2008 Hyper-V
Version 2.12e of the DMTF standard Concrete subclasses use the Msvm_ prefix
Snapshots Point-in-time representation of a VM
Configuration Disk Runtime state
Applying a snapshot Snapshot is unaffected “Current” configuration for the VM is overwritten Disk and runtime state are copied
Windows Server 2008 Hyper-V
Version 2.12e of the DMTF standard Concrete subclasses use the Msvm_ prefix
Snapshots Point-in-time representation of a VM
Configuration Disk Runtime state
Applying a snapshot Snapshot is unaffected “Current” configuration for the VM is overwritten Disk and runtime state are copied
Typically read-only
Windows Server 2008 Hyper-V
Virtual Machine Object Model
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
ComputerSystem
Virtual Machine(Child Partition)
LogicalDevice
Virtual Device in the VM
VirtualSystemSettingData
Current Settings for the Virtual Machine
SystemDevice VirtualSystemSettingDataComponent
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
ElementSettingData
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for the Virtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
VirtualSystemGlobalSettingData
Global Settings for the Virtual Machine
SettingsDefineState
Virtual Machine
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
VirtualSystemSettingData
Current Settings for the Virtual Machine
SystemDevice VirtualSystemSettingDataComponent
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
ElementSettingData
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for the Virtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
VirtualSystemGlobalSettingData
Global Settings for the Virtual Machine
SettingsDefineState
ComputerSystem
Virtual Machine(Child Partition)
Virtual Machine Settings
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
ComputerSystem
Virtual Machine(Child Partition)
LogicalDevice
Virtual Device in the VM
SystemDevice VirtualSystemSettingDataComponent
SettingsDefineState
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for the Virtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
VirtualSystemGlobalSettingData
Global Settings for the Virtual Machine
SettingsDefineState
VirtualSystemSettingDataCurrent Settings for the
Virtual Machine
VirtualSystemSettingDataSnapshot Settings for the
Virtual Machine
ElementSettingData
Global Virtual Machine Settings
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
ComputerSystem
Virtual Machine(Child Partition)
LogicalDevice
Virtual Device in the VM
VirtualSystemSettingData
Current Settings for the Virtual Machine
SystemDevice VirtualSystemSettingDataComponent
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
ElementSettingData
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for the Virtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
SettingsDefineState
VirtualSystemGlobalSettingData
Global Settings for theVirtual Machine
Virtual Device
ComputerSystem
Virtual Machine(Child Partition)
VirtualSystemSettingData
Current Settings for the Virtual Machine
SystemDevice VirtualSystemSettingDataComponent
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
ElementSettingData
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for the Virtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
VirtualSystemGlobalSettingData
Global Settings for the Virtual Machine
SettingsDefineState
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
Virtual Device Settings
SettingsDefineState
LogicalDevice
Virtual Device in the VM
LogicalDevice
Virtual Device in the VM
ComputerSystem
Virtual Machine(Child Partition)
LogicalDevice
Virtual Device in the VM
VirtualSystemSettingData
Current Settings for theVirtual Machine
SystemDeviceVirtualSystem
SettingDataComponent
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for theVirtual Machine
ElementSettingData
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Current Settings for theVirtual Device
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
VirtualSystemGlobalSettingData
Global Settings for theVirtual Machine
Msvm_ComputerSystem Represents either a physical or virtual machine
Msvm_HostedDependency Antecedent – Physical Machine Dependent – Virtual Machine
Virtual Machine
ComputerSystem
Virtual Machine(Child Partition)
HostedDependency
ComputerSystem
Physical Machine(Parent Partition)
Msvm_ComputerSystem EnabledState RequestStateChange()
“Enabled” “Disabled” “Paused” “Suspended” “Reset”
ProcessID
Virtual Machine
Msvm_VirtualSystemSettingData (VSSD) Current or snapshot settings for the VM
Associations Msvm_SettingsDefineState Msvm_ElementSettingData
SettingType Current = ‘3’ Snapshot = ‘5’
Virtual Machine Settings
ComputerSystem
Virtual Machine(Child Partition)
VirtualSystemSettingData
Current Settings for the Virtual Machine
SettingsDefineState
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
ElementSettingData
Msvm_VirtualSystemSettingData BIOS settings NUMA settings Notes Parent
Msvm_VirtualSystemGlobalSettingData ElementName Automatic startup/shutdown/recovery actions ScopeOfResidence ExternalDataRoot SnapshotDataRoot
Virtual Machine Settings
Msvm_VirtualSystemManagementService Methods for managing virtual machine elements
Virtual machines Devices Snapshots
Virtual Machine Management
Msvm_VirtualSystemManagementService Methods for managing virtual machine elements
Virtual machines Devices Snapshots
Embedded instances Intrinsic ‘Put’ and ‘Delete’ operations not supported CIM-XML format .NET -
ManagementObject.GetText(TextFormat.WmiDtd20) PowerShell - $obj.PsBase.GetText(1)
Virtual Machine Management
Asynchronous operations Return code
0 – Succeeded synchronously 4096 – Operation started
CIM_ConcreteJob object indicates progress, completion status, and rich error information
Virtual Machine Management
Asynchronous operations Return code
0 – Succeeded synchronously 4096 – Operation started
CIM_ConcreteJob object indicates progress, completion status, and rich error information
Any other return code indicates an error Invalid parameter Insufficient memory Invalid virtual machine state
Virtual Machine Management
Create Virtual Machine
# Get Msvm_VirtualSystemGlobalSettingData class definition$wmiClassString = "\\svr\root\virtualization:Msvm_VirtualSystemGlobalSettingData"$wmiClass = [WMIClass]$wmiClassString
Create Virtual Machine
# Get Msvm_VirtualSystemGlobalSettingData class definition$wmiClassString = "\\svr\root\virtualization:Msvm_VirtualSystemGlobalSettingData"$wmiClass = [WMIClass]$wmiClassString
#Create a local instance of the class$vsgsd = $wmiClass.CreateInstance()$vsgsd.psbase.Properties.Item("ElementName").value = "MyVM"
Create Virtual Machine
# Get Msvm_VirtualSystemGlobalSettingData class definition$wmiClassString = "\\svr\root\virtualization:Msvm_VirtualSystemGlobalSettingData"$wmiClass = [WMIClass]$wmiClassString
#Create a local instance of the class$vsgsd = $wmiClass.CreateInstance()$vsgsd.psbase.Properties.Item("ElementName").value = "MyVM"
# Serialize $vsgsd into an embedded instance$embeddedInstance = $vsgsd.psbase.GetText(1)
Create Virtual Machine
# Get Msvm_VirtualSystemGlobalSettingData class definition$wmiClassString = "\\svr\root\virtualization:Msvm_VirtualSystemGlobalSettingData"$wmiClass = [WMIClass]$wmiClassString
#Create a local instance of the class$vsgsd = $wmiClass.CreateInstance()$vsgsd.psbase.Properties.Item("ElementName").value = "MyVM"
# Serialize $vsgsd into an embedded instance$embeddedInstance = $vsgsd.psbase.GetText(1)
# Get Msvm_VirtualSystemManagementService instance$managementService = gwmi Msvm_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName “svr“
Create Virtual Machine
# Get Msvm_VirtualSystemGlobalSettingData class definition$wmiClassString = "\\svr\root\virtualization:Msvm_VirtualSystemGlobalSettingData"$wmiClass = [WMIClass]$wmiClassString
#Create a local instance of the class$vsgsd = $wmiClass.CreateInstance()$vsgsd.psbase.Properties.Item("ElementName").value = "MyVM"
# Serialize $vsgsd into an embedded instance$embeddedInstance = $vsgsd.psbase.GetText(1)
# Get Msvm_VirtualSystemManagementService instance$managementService = gwmi Msvm_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName "svr"
# Create the virtual machine$result = $managementService.DefineVirtualSystem( $embeddedInstance, $null)
ConcreteJob Handling
# If ReturnValue = 4096, the operation is handled asynchronouslyif ($result.ReturnValue -eq 4096){}
ConcreteJob Handling
# If ReturnValue = 4096, the operation is handled asynchronouslyif ($result.ReturnValue -eq 4096){ $job=[WMI]$result.job
# Loop while JobState = 3 ("Starting") or 4 ("Running") while ($job.JobState -eq 3 -or $job.JobState -eq 4) { }}
ConcreteJob Handling
# If ReturnValue = 4096, the operation is handled asynchronouslyif ($result.ReturnValue -eq 4096){ $job=[WMI]$result.job
# Loop while JobState = 3 ("Starting") or 4 ("Running") while ($job.JobState -eq 3 -or $job.JobState -eq 4) { write-host $job.PercentComplete start-sleep 1 $job=[WMI]$Result.job }}
ConcreteJob Handling
# If ReturnValue = 4096, the operation is handled asynchronouslyif ($result.ReturnValue -eq 4096){ $job=[WMI]$result.job
# Loop while JobState = 3 ("Starting") or 4 ("Running") while ($job.JobState -eq 3 -or $job.JobState -eq 4) { write-host $job.PercentComplete start-sleep 1 $job=[WMI]$Result.job }
# Indicate whether the operation succeeded if ($job.JobState -eq 7) write-host "Virtual machine created.“ else write-host $job.GetError}
CIM_LogicalDevice Only surfaced when the VM is powered on
Running/Paused/Transitional states Associations
Msvm_SystemDevice Msvm_SettingsDefineState
Emulated versus Synthetic devices
Virtual Devices
LogicalDevice
Virtual Device in the VM
SystemDevice
ComputerSystem
Virtual Machine(Child Partition)
CIM_ResourceAllocationSettingData (RASD) Current or snapshot settings for a device Associated with “current” or “snapshot”
instance of Msvm_VirtualSystemSettingData
Virtual Device Settings
VirtualSystemSettingData
Current Settings for theVirtual Machine
VirtualSystemSettingDataComponent
VirtualSystemSettingData
Snapshot Settings for the Virtual Machine
VirtualSystemSettingDataComponent
ResourceAllocationSettingData
Snapshot Settings for the Virtual Device
ResourceAllocationSettingData
Current Settings for theVirtual Device
CIM_ResourceAllocationSettingData Always present in a VM Device type
ResourceType ResourceSubType OtherResourceType
Virtual Device Settings
CIM_ResourceAllocationSettingData Always present in a VM Device type
ResourceType ResourceSubType OtherResourceType
Virtual machine connectivity Parent Address
Virtual Device Settings
CIM_ResourceAllocationSettingData Always present in a VM Device type
ResourceType ResourceSubType OtherResourceType
Virtual machine connectivity Parent Address
Physical machine connectivity HostResource[] Connection[]
Virtual Device Settings
Resource Pool Object Model
AllocationCapabilities
Capabilities for the Virtual Resources in the Pool
ResourceAllocationSettingData
Default Values for Each Property
ResourcePool
Pool of Resources Which May Be Allocated to a VM
ResourceAllocationSettingData
Minimum Values for Each Property
ResourceAllocationSettingData
Maximum Values for Each Property
ResourceAllocationSettingData
Increment Values for Each Property
ElementCapabilities
SettingsDefineCapabilities
Get instance of CIM_ResourcePool Navigate to “Default” RASD Modify RASD values
Respect minimum, maximum, and increment values specified in the other RASD instances
Resource Pool Usage
Get instance of CIM_ResourcePool Navigate to “Default” RASD Modify RASD values
Respect minimum, maximum, and increment values specified in the other RASD instances
Convert RASD into an embedded instance
Resource Pool Usage
Get instance of CIM_ResourcePool Navigate to “Default” RASD Modify RASD values
Respect minimum, maximum, and increment values specified in the other RASD instances
Convert RASD into an embedded instance Call AddVirtualSystemResources method
to add the device to a virtual machine
Resource Pool Usage
Pass-through Storage Controller Disk Drive
Connected directly to physical storage volume
Storage Types
Pass-through Storage Controller Disk Drive
Connected directly to physical storage volume Virtual Hard Disk (VHD)
Controller Disk Drive Logical Disk
Connected to VHD file
Storage Types
Get Existing IDE Controller
# Get the virtual machine object$Vm = Get-WmiObject -Namespace "root\virtualization" –Query "SELECT * From Msvm_ComputerSystem WHERE ElementName='MyVM'"
Get Existing IDE Controller
# Get the virtual machine object$Vm = Get-WmiObject -Namespace "root\virtualization" –Query "SELECT * From Msvm_ComputerSystem WHERE ElementName='MyVM'"
# Get the current VM settings object$Vssd = Get-WmiObject -Namespace "root\virtualization" –Query "ASSOCIATORS OF {$Vm} WHERE AssocClass=Msvm_SettingsDefineState ResultClass=Msvm_VirtualSystemSettingData"
Get Existing IDE Controller
# Get the virtual machine object$Vm = Get-WmiObject -Namespace "root\virtualization" –Query "SELECT * From Msvm_ComputerSystem WHERE ElementName='MyVM'"
# Get the current VM settings object$Vssd = Get-WmiObject -Namespace "root\virtualization" –Query "ASSOCIATORS OF {$Vm} WHERE AssocClass=Msvm_SettingsDefineState ResultClass=Msvm_VirtualSystemSettingData"
# Get the settings for IDE controller 0 on the VM$IdeRasd = (Get-WmiObject -Namespace "root\virtualization" -Query "ASSOCIATORS OF {$Vssd} WHERE AssocClass=Msvm_VirtualSystemSettingDataComponent ResultClass=Msvm_ResourceAllocationSettingData" | Where-Object {$_.ResourceType –eq 5 -and $_.Address –eq 0})
Get Default Disk Drive Settings
$DrivePool = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_ResourcePool WHERE ResourceType=22 AND ResourceSubType='Microsoft Synthetic Disk Drive'"
Get Default Disk Drive Settings
$DrivePool = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_ResourcePool WHERE ResourceType=22 AND ResourceSubType='Microsoft Synthetic Disk Drive'"
$DriveCaps = Get-WmiObject –Namespace "root\virtualization" –Query "ASSOCIATORS OF {$DrivePool} WHERE AssocClass=Msvm_ElementCapabilities ResultClass=Msvm_AllocationCapabilities"
Get Default Disk Drive Settings
$DrivePool = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_ResourcePool WHERE ResourceType=22 AND ResourceSubType='Microsoft Synthetic Disk Drive'"
$DriveCaps = Get-WmiObject –Namespace "root\virtualization" –Query "ASSOCIATORS OF {$DrivePool} WHERE AssocClass=Msvm_ElementCapabilities ResultClass=Msvm_AllocationCapabilities"
$DriveRasd = Get-WmiObject -Namespace "root\virtualization" -Query "REFERENCES OF {$DriveCaps} WHERE ResultClass=Msvm_SettingsDefineCapabilities" | Where-Object {$_.ValueRole -eq 0} | ForEach-Object {[Wmi]$_.PartComponent})
Configure And Add Disk Drive
# Configure the drive as the first device on the IDE controller$DriveRasd.Parent = $IdeRasd.__Path$DriveRasd.Address = 0
Configure And Add Disk Drive
# Configure the drive as the first device on the IDE controller$DriveRasd.Parent = $IdeRasd.__Path$DriveRasd.Address = 0
# The AddVirtualSystemResources method takes an array of embedded instances of CIM_ResourceAllocationSettingData$RasdArray = [string[]]@($DriveRasd.PSBase.GetText(1))
Configure And Add Disk Drive
# Configure the drive as the first device on the IDE controller$DriveRasd.Parent = $IdeRasd.__Path$DriveRasd.Address = 0
# The AddVirtualSystemResources method takes an array of embedded instances of CIM_ResourceAllocationSettingData$RasdArray = [string[]]@($DriveRasd.PSBase.GetText(1))
# Add the disk drive to the virtual machine$result = $ManagementService.AddVirtualSystemResources($Vm, $RasdArray)
Pass-through Storage HostResource[0] property on disk drive RASD
Virtual Hard Disk Add LogicalDisk device
Attaching Storage To A Disk Drive
Pass-through Storage HostResource[0] property on disk drive RASD
Virtual Hard Disk Add LogicalDisk device
Find ResourcePool for VHDs ResourceType = 21 (“Storage Extent”) ResourceSubType =
“Microsoft Virtual Hard Disk”
Attaching Storage To A Disk Drive
Pass-through Storage HostResource[0] property on disk drive RASD
Virtual Hard Disk Add LogicalDisk device
Find ResourcePool for VHDs ResourceType = 21 (“Storage Extent”) ResourceSubType =
“Microsoft Virtual Hard Disk” Navigate to “Default” RASD
Attaching Storage To A Disk Drive
Pass-through Storage HostResource[0] property on disk drive RASD
Virtual Hard Disk Add LogicalDisk device
Find ResourcePool for VHDs ResourceType = 21 (“Storage Extent”) ResourceSubType =
“Microsoft Virtual Hard Disk” Navigate to “Default” RASD Configure settings
Parent = Object path of disk drive RASD Connection[0] = File system path to VHD file
Attaching Storage To A Disk Drive
Pass-through Storage HostResource[0] property on disk drive RASD
Virtual Hard Disk Add LogicalDisk device
Find ResourcePool for VHDs ResourceType = 21 (“Storage Extent”) ResourceSubType =
“Microsoft Virtual Hard Disk” Navigate to “Default” RASD Configure settings
Parent = Object path of disk drive RASD Connection[0] = File system path to VHD file
Call AddVirtualSystemResources
Attaching Storage To A Disk Drive
ComputerSystem
Virtual Machine(Child Partition)
LogicalDevice
IDE Controller
VirtualSystemSettingData
Current Settings for the Virtual Machine
SettingsDefineState
SettingsDefineState
ResourceAllocationSettingData
Settings for the IDE Controller
LogicalDevice
Disk Drive
LogicalDevice
Logical Disk(Virtual Hard Disk)
ResourceAllocationSettingData
Settings for the Disk Drive
ResourceAllocationSettingData
Settings for the Logical Disk
VirtualSystemSettingDataComponent
SettingsDefineState
SettingsDefineState
SystemDevice
Attaching Storage To A Disk Drive
Networking Object Model
External (Physical)Internal (Parent)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch Device
HostedAccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
Networking Object Model
External (Physical)Internal (Parent)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch DeviceHosted
AccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
Networking Object Model
External (Physical)Internal (Parent)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch Device
HostedAccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
Networking Object Model
Internal (Parent)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch Device
HostedAccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
External (Physical)
External (Physical)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch Device
HostedAccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
Internal (Parent)
Networking Object Model
Networking Object Model
External (Physical)Internal (Parent)Virtual MachineComputerSystem
Virtual Machine(Child Partition)
EthernetPort
Virtual NIC Device
SystemDevice
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
SwitchPort
Port on the Virtual Switch
VirtualSwitch
Virtual Switch Device
HostedAccessPoint
VmLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
DeviceSAPImplementation
ActiveConnection
SwitchPort
Port on the Virtual Switch
ExternalEthernetPort
Physical NIC Device
SwitchLANEndpoint
Protocol Endpoint Used To Connect Two Networking Ports
InternalEthernetPort
Internal NIC Used to Describe Connectivity into the Host OS
HostedAccessPoint
SwitchPort
Port on the Virtual Switch
GlobalEthernetPortSAPImplementation
ActiveConnection ActiveConnection
GlobalEthernetPortSAPImplementation
Create Virtual Switch
# Get an instance of the virtual switch management service$VirtualSwitchService = get-wmiobject –class "Msvm_VirtualSwitchManagementService" –namespace "root\virtualization" -computername "MyServer"
Create Virtual Switch
# Get an instance of the virtual switch management service$VirtualSwitchService = get-wmiobject –class "Msvm_VirtualSwitchManagementService" –namespace "root\virtualization" -computername "MyServer“
# Create a virtual switch$ReturnObject = $VirtualSwitchService.CreateSwitch( [guid]::NewGuid().ToString(), "My Switch", 1024, "")$CreatedSwitch = [WMI]$ReturnObject.CreatedVirtualSwitch
Create Switch Ports
# Create internal switch port$ReturnObject = $VirtualSwitchService.CreateSwitchPort($CreatedSwitch, [guid]::NewGuid().ToString(), "Internal Switch Port", "")$InternalSwitchPort = [WMI]$ReturnObject.CreatedSwitchPort
Create Switch Ports
# Create internal switch port$ReturnObject = $VirtualSwitchService.CreateSwitchPort($CreatedSwitch, [guid]::NewGuid().ToString(), "Internal Switch Port", "")$InternalSwitchPort = [WMI]$ReturnObject.CreatedSwitchPort
# Create external switch port$ReturnObject = $VirtualSwitchService.CreateSwitchPort($CreatedSwitch, [guid]::NewGuid().ToString(), "External Switch Port", "")$ExternalSwitchPort = [WMI]$ReturnObject.CreatedSwitchPort
# Create VM switch port$ReturnObject = $VirtualSwitchService.CreateSwitchPort($CreatedSwitch, [guid]::NewGuid().ToString(), "MyVm Switch Port", "")$VmSwitchPort = [WMI]$ReturnObject.CreatedSwitchPort
Setup Switch
# Find an unbound physical NIC$PhysicalNic = get-wmiobject -namespace "root\virtualization" -Query "SELECT * FROM Msvm_ExternalEthernetPort WHERE IsBound=False" -computername "MyServer" | select –first 1
Setup Switch
# Find an unbound physical NIC$PhysicalNic = get-wmiobject -namespace "root\virtualization" -Query "SELECT * FROM Msvm_ExternalEthernetPort WHERE IsBound=False" -computername "MyServer" | select –first 1
# Call SetupSwitch to create internal and external connectivity$result = $VirtualSwitchService.SetupSwitch( $ExternalSwitchPort, $InternalSwitchPort, $PhysicalNic, [guid]::NewGuid().ToString(), "Internal Ethernet Port")
Connect Virtual Machine
$NicPool = Get-WmiObject -Namespace "root\virtualization" -Query "SELECT * FROM Msvm_ResourcePool WHERE ResourceType=10 AND ResourceSubType='Microsoft Emulated Ethernet Port'"
$NicCaps = Get-WmiObject –Namespace "root\virtualization" –Query "ASSOCIATORS OF {$NicPool} WHERE AssocClass=Msvm_ElementCapabilities ResultClass=Msvm_AllocationCapabilities"
$NicRasd = Get-WmiObject -Namespace "root\virtualization" -Query "REFERENCES OF {$NicCaps} WHERE ResultClass=Msvm_SettingsDefineCapabilities" | Where-Object {$_.ValueRole -eq 0} | ForEach-Object {[Wmi]$_.PartComponent})
Connect Virtual Machine
# Configure the settings for the virtual NIC$NicRasd.Connection[0] = $VmSwitchPort.__Path
# The AddVirtualSystemResources method takes an array of embedded instances of CIM_ResourceAllocationSettingData$RasdArray = [string[]]@($NicRasd.PSBase.GetText(1))
# Add the virtual NIC to the VM$result = $ManagementService.AddVirtualSystemResources($Vm, $RasdArray)
Additional sessions Enabling Test Automation Using
Windows Server 2008 Hyper-V Thursday, Oct. 30th, 12:00 – 1:15 PM Room 403AB
Visit the hands-on-lab! Get hands on experience programming against
the Hyper-V WMI APIs with members of the Hyper-V team to answer your questions
Following Up
Evals & Recordings
Please fill
out your
evaluation for
this session at:
This session will be available as a recording at:
www.microsoftpdc.com
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.