| Profiel van Dung K HoangDung's spaceWeblogNetwerk | Help |
|
|
27 mei Hyper-V WMI Examples - Part XV
Creating a new virtual machine from an existing virtual hard diskDue to numerous requests, I dedicate this entry to talk about creating a virtual machine from an existing virtual hard disk. It seems that some of you were not able to get it working using the example of adding a DVD as resource to an existing VM. I must admit that it's not intuitive and I still have to do it several times to make it right.
GoalCreate a new virtual machine based on an existing virtual hard disk. The VHD file is called C:\W2K3.VHD and the disk will be created on IDE Controller 0 at location 0. High level steps
Script$VHDFile = "C:\W2K3.vhd" # Step 1 $VM_Service = get-wmiobject –namespace root\virtualization Msvm_VirtualSystemManagementService if ($status.ReturnValue -eq 0) # Step 2 $ListOfControllers = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData ` # Step 3 $DiskDefault = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | ` # Step 4 $VHDDefault = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | `
Enjoy! /Dung 17 mei Hyper-V WMI Examples - Part XIV
Hyper-V Virtual Networks - Connecting virtual machines to a virtual networkNow that you know how to create virtual networks, let me show how to connect a NIC of a given virtual machine to a network The script below is extracted from a discussion in the Virtualization Technet Forum. All credits go to AggieMatt! ## Assumes that you already identify a virtual switch and store it in a variable called $Switch ## Create a new SwitchPort Enjoy! /Dung Hyper-V WMI Examples - Part XII
Hyper-V Virtual Networks - Creating External Virtual NetworksFinally let's see how you can create an External virtual network. Here is an overview of high level steps: External Virtual Network
Script# Step 0: Connect to the Virtual Network Management Service # Step 1 ## Change the name used above to the name of your network adapter. # Step 2 # Store the path for later use # Step 3: Create two ports on the switch # Store the path for later use # Port for External # Store the path for later use # Step 4 Enjoy! /Dung 15 mei Hyper-V WMI Examples - Part XI
Hyper-V Virtual Networks - Creating Internal Virtual NetworksLet's talk about how to create Internal Virtual Networks. In my previous post, I described the concept and high level steps to create internal virtual network. Steps to create a Internal Virtual Network
Script$SW_Service = get-wmiobject –namespace root\virtualization Msvm_VirtualSwitchManagementService ## Step 1 $Status = $Sw_Service.CreateInternalEthernetPort("Internal Network Adapter", "Internal NIC", "020304050607" ) if ($Status.ReturnValue -eq 0)
## Step 2 $NIC_Port = [WMI]$InternalEtherPortPATH # Find the LANEndPoint associated to this NIC ## Step 3 $Status = $SW_Service.CreateSwitch(“Internal_Network”, “Host and Guests Virtual Network”, 1024, “”) if ($Status.ReturnValue -eq 0)
## Step 4 $Status = $SW_Service.CreateSwitchPort($SwitchPATH, "Internal_Port" , "Internal Port for Host") if ($Status.ReturnValue -eq 0)
## Step 5 $Status = $SW_Service.ConnectSwitchPort($PortPATH , $LANEndPoint) ## Additional Step - Create a port to connect VM $Status = $SW_Service.CreateSwitchPort($SwitchPATH, "One_Port" , "A Switch Port to connect VM") if ($Status.ReturnValue -eq 0)
Enjoy! /Dung 13 mei Hyper-V WMI Examples - Part XHyper-V Virtual Networks - Creating Private Virtual NetworksIn a previous post, I give you an overview of virtual networks and high-level steps to create virtual networks. Let's put it in practice today and show how to create a private virtual network. Steps to create a Private virtual network
Script$SW_Service = get-wmiobject –namespace root\virtualization Msvm_VirtualSwitchManagementService # Call CreateSwitch $Status = $SW_Service.CreateSwitch(“Private_Network”, “Guest only Virtual Network”, 1024, “”) if ($Status.ReturnValue -eq 0)
## Create a SwitchPort $Status = $SW_Service.CreateSwitchPort($SwitchPATH, "One_Port" , "A Switch Port to connect VM") if ($Status.ReturnValue -eq 0)
Enjoy! /Dung 12 mei Hyper-V WMI Examples - Part IX
Hyper-V Virtual NetworksUp to now, we've spent a significant amount of time and blog entries to talk about virtual machines. Let's switch gear and discuss about virtual networks. Networking ConceptsIn Hyper-V, you create virtual networks and connect virtual network cards (NICs) of virtual machines to allow communications between the machines themselves, the host system and eventually the physical network. A virtual network, also called Virtual Switch can be one of the three following types: · Private: Virtual machines connected to this network can communicate between them. The host system has no connectivity with virtual machines · Internal: Virtual machines connected to this network can communicate between themselves and the host system. There is no connectivity with the physical network. · External: An external virtual network binds to the physical network adapter so that virtual machines as well as the host system can access the physical network. Virtual networks and connections of virtual machines to virtual networks are managed by a Hyper-V service called Virtual Switch Management Service. There is a WMI class associated to this service called Msvm_VirtualSwitchManagementService. Private Virtual NetworkWhen you create a private virtual network, the Virtual Network Manager Service running on the host system will create a Virtual Switch and allows you to connect virtual machines to this switch. The host system does not participate in this private network, as such, cannot communicate with virtual machines connected to it. This type of network is also known as "Guest-Only Network" or "Private Virtual Machine Network". The figure below shows a visual representation of a private virtual network.
Internal Virtual NetworkWhen you create an Internal virtual network, the Virtual Network Manager Service running on the host system will create: · A virtual network · A virtual NIC on the host system that will be connected to the virtual network. As such, all virtual machines connected to the Internal virtual network can communicate with the host system and vice-versa. It is important to note that existing physical network adapters are left untouched. The figure below shows a visual representation of an internal virtual network.
External Virtual NetworkWhen you create an External virtual network, you specify a physical network adapter for the virtual network to bind to. As a consequence, the Virtual Network Manager Service running on the host system will create: · A virtual network · A virtual NIC on the host system that will be connected to the virtual network The physical network adapter will then be connected to the virtual network. The latter will be acting as a router and allow both virtual machines and host system to connect to the physical network. Figure 3 illustrates the concept of an external virtual network. It's interesting to note that:
The figure below illustrates the concept of an external virtual network.
How to connect a virtual machine to a virtual network?Now that you understand the concept of virtual network, your next question would be to connect a virtual machine to a virtual network. Assume that your virtual machine already has a NIC and that you have created a virtual network, you will need to create a port on the virtual network, or to be exact, create a SwitchPort, and then associate this SwtichPort to a NIC by setting the Connection property of the NIC to the WMI Path of the SwitchPort. If your virtual machine does not have a NIC, you need to create one by duplicating the default instance of Msvm_SyntheticEthernetPortSettingData and perform the same operations as above. Overview of Msvm_VirtualSwitchManagementServiceThe Msvm_VirtualSwitchManagementService class is used to control the creation and configuration of virtual networks. As you can see from the explanations above, there are two distinct tasks in virtual networks management. First you create a virtual network and define its type – private, internal, external. Once the virtual network created, you then create ports on the switch for network connections. Second you connect virtual NICs (either from virtual machines or the host system) to a virtual network. The Msvm_VirtualSwitchManagementService class provides several methods to create virtual networks and configure its type. Creating a virtual network is very easy and consists of a single-step operation. To configure the type, you may need additional steps. Here is a high-level overview of steps to create and configure virtual networks. All the methods listed below come from the Msvm_VirtualSwitchManagementService class. Private Virtual Network
Internal Virtual Network
External Virtual Network
That's it! Until next time... /Dung 09 mei Hyper-V WMI Examples - Part VIII
Adding Resources of a VMTo continue from the previous post, let's discuss how you can add a resource to a VM. In this scenario, I want to add a new DVD drive to my VM virtual machine and attach an ISO file called c:\ISO\W2K3.ISO. When you explore settings of a given VM using the Hyper-V Manager console, you notice that a VM can have up to 2 IDE controllers (0,1) and each IDE controller has 2 slots (0,1). You can add either a disk drive or a DVD drive to each of the IDE controllers. You also notice that you can only add a drive (disk or DVD) when selecting a controller and cannot use the generic action "Add Hardware" to add a drive. The script should behave exactly like the GUI interface, meaning that I have to provide the IDE controller number and slot as parameters when creating a drive. Enough talk , let's do real work :=) Note: Script updated based on James Logan's feedback. Assume that I want to add a DVD drive to my VM on IDE Controller 1 at location 1 -------------------Start of Script to Add a resource ------------------------------------------------ VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService # Step 1; Locate an IDE controller and an available location $ListOfControllers = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | ` foreach ($Controller in ListOfControllers) if ($Controller.Address -eq 1) # Step 2: Create a synthetic DVD drive $DVD_Default = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | ` $NewDVDDrive = $DVD_Default.psbase.Clone() ## So fill out the necessary information $NewDVDDrive.Parent = $IDEController1.__PATH $NewDVDDrive.Address = 1 ## Apply the changes $VM_Service.AddVirtualSystemResources($VM.__PATH, $NewDVDDrive.psbase.Gettext(1)) #Step 3: Create a CD/DVD disk # First locate the new DVD drive created in previous step. $NewDVD = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | ` # Create a CD/DVD disk. To create a new disk you will search for the Default instance object and clone it. $CD_Default = get-wmiobject -namespace root\virtualization Msvm_ResourceAllocationSettingData | ` $CDDisk = $CD_Default.psbase.Clone() # Apply the changes -------------------End of Script to Add a resource ---------------------------------------------- Done! Until the next time ...... /Dung Hyper-V WMI Examples - Part VII
Modifying Resources of a VMNow that you know how to query WMI to get list of resources associated to a VM, let's discuss how you can modify a resource. Modifying the location of VHD fileLet's say that my VM has a VHD file named C:\VMs\MyVM.VHD and I want to move the location of the file from C: to L: drive. After copying the VHD file to the new location, here is the script to change the location of the disk image file for the VM -------------------Start of Script to modify a resource ------------------------------------------------ VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ## First you need to locate the active settings of a virtual machine ## Then find all the resources allocated to this VM. ## IN this scenario, I'm interested only on the resources whose Subtype is "Microsoft Virtual Hard Disk " foreach ($item in $VResourceComponents)
if ($Res_VHD -ne $NULL) { $Res_VHD.Connection = $Res_VHD.Connection -replace "C:", "L:"
-------------------End of Script to modify a resource ------------------------------------------------ Enjoy! /Dung 04 mei Hyper-V WMI Examples - Part VI
Today I will talk about finding resources of virtual machine.
Now that you understand how a resource is represented in Hyper-V, the next question is how those resources are attached to a specific Virtual Machine (VM). First all virtualization-specific settings of each VM is represented by an instance of the class Msvm_VirtualSystemSettingData. The class describes a kind of "motherboard" of a VM. It has properties like BIOS, BaseBoardSerialNumber... Do note that a snapshot is also represented by an instance of the same class, it means that a snapshot is also viewed as a virtual machine of a specific type. Once you get the Msvm_VirtualSystemSettingData, you can find all resources associated to a VM by querying the class Msvm_VirtualSystemSettingDataComponent. The query returns all associations between an instance of Msvm_VirtualSystemSettingData and one or more instances of Msvm_ResourceAllocationSettingData. This is how you can find resources that are allocated to a VM. You need to go through Msvm_VirtualSystemSettingData and then Msvm_VirtualSystemSettingDataComponet So let's put it in practice: $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService ## First you need to locate the active settings of a virtual machine ## Then find all the resources allocated to this VM. ## Now we have all resources associated to a VM, let's enumerate them foreach ($item in $VResourceComponents)
Et Voilà! /Dung |
|
|