Profiel van Dung K HoangDung's spaceWeblogNetwerk Extra Help

Weblog


    16 juni

    TechNet Webcast on Hyper-V WMI

     

    I will deliver a TechNet webcast next month to talk on Hyper-V WMI and PowerShell

    Here is the link for register

    http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032381324&EventCategory=4&culture=en-US&CountryCode=US

    Webcast Name: Managing Hyper-V Virtual Machines with WMI and Windows PowerShell (Level 300)

    Date: 7/2/2008 08:00 AM PST – 09:00 AM PST

     

    See you there!

    /Dung

    HyperV WMI PowerShell

    13 juni

    Hyper-V WMI Examples – Part XIX

     

    Virtual Machine creation – Revisiting the topic again

    Since I start the blog, I have seen many questions on how to create a new virtual machine and define resources such as memory, NICs at creation time. I must admit that the same question is in the back of my mind since a while and after looking at this blog entry of the Virtual PC guy, http://blogs.msdn.com/virtual_pc_guy/archive/2008/05/28/scripting-vm-creation-with-hyper-v.aspx, I decide to re-visit the topic and cover multiple scenarios here.

    In order to create a new virtual machine in Hyper-V, you must use the method DefineVirtualSystem  of the Msvm_VirtualSystemManagementService object. This method accepts 3 parameters:

    • SystemSettingData: an instance of Msvm_VirtualSystemGlobalSettingData
    • ResourceSettingData: an array of instances of Msvm_ResourceAllocationSettingData. Each instance represents a resource you want to add the virtual machine to be created.
    • SourceSetting: Refers to an existing Msvm_VirtualSystemSettingData that is used as template for creation of the VM

    It is quite intimidating for those who just want to create a virtual machine with resources pre-defined for this VM, but if you read this blog since the beginning, you will see that it’s quite simple :=).

    Scenario 1: Creating a blank VM

    I notice that if you call this method without any parameter, it will create a blank virtual machine! ( although the documentation mentions that the 1st parameter is mandatory). So in its simplest form, creating a VM can be achieved with 3 lines of scripts:

    Script

    $Server = “localhost”
    $VM_Service = get-wmiobject -computername $server -namespace root\virtualization Msvm_VirtualSystemManagementService

    $VM_Service.DefineVirtualSystem()

    The virtual machine will be created and displayed as “New Virtual Machine”

     

    Scenario 2: Creating a VM with a pre-defined name

    The blog post mentioned above shows how to create a new VM and specify a new display name at time of creation. I will not reproduce the code here but basically you create a new instance of the class MSvm_VirtualSystemGlobalSettingData, change the display name and then call DefineVirtualSystem.

     

    Scenario 3: Creating a VM and specifying resources

    In this scenario, I want to create a virtual machine with two NIC cards. For this, I will be leveraging some scripts developed/ shown in Hyper-V WMI Examples - Part XIV for creating network adapters. The first NIC will have static MAc Address while the second one will use dynamic MAC address.

    Script

    $Server = “localhost”
    $VM_Service = get-wmiobject -computername $server -namespace root\virtualization Msvm_VirtualSystemManagementService

    $VMGlobalSettingClass = [WMIClass]”\\Localhost\root\virtualization:Msvm_VirtualSystemGlobalSettingData"

    $NewGS = $VMGlobalSettingClass.psbase.CreateInstance()

    ## Now Create 2 NICs

    $GUID1 = [GUID]::NewGUID().ToString()
    $GUID2 = [GUID]::NewGUID().ToString()

    $DefaultNIC = gwmi -namespace root/virtualization Msvm_SyntheticEthernetPortSettingData | where {$_.InstanceID -like "*Default*"}

    $StaticNIC = DefaultNIC.psbase.Clone()
    $StaticNIC.VirtualSystemIdentifiers = “{$GUID1}”
    $StaticNIC.StaticMacAddress = $true
    $StaticNIC.Address = “00155D929001”

    $DynamicNIC = DefaultNIC.psbase.Clone()
    $DynamicNIC.VirtualSystemIdentifiers = “{$GUID2}”

    ## Build an array of resources as required by DefineVirtualSystem

    $RASD = @()

    $RASD += $StaticNIC.psbase.gettext(1)
    $RASD += $SDynamicNIC.psbase.gettext(1)

    ## Finally call DefineVirtualSystem

    $VM_Service.DefineVirtualSystem($NewGS.__PATH, $RASD)

    Et Voilà!

    Now that you understand the process, you can leverage my other examples to add DVD drive, new hard disks when creating virtual machines. I will leave it to you as homework for this weekend!

     

    Enjoy!

    /Dung

    HyperV WMI PowerShell

    11 juni

    Hyper-V WMI Examples – Part XVIII

     

    How to find list of VMs connected to a given switch?

    In MS Virtual Server 2005, when looking at a virtual network, you can easily find a list of VMs that are attached to this network. Well, there is no easy way to find it within Hyper-V unless ….

    Andy Schneider has asked the  same question on getting a list of connected VMs per switch. So here is the result. Enjoy!

    Steps
    1. Get a list of switches
    2. Find all Switch ports created on a given switch
    3. Get all instances of Msvm_ActiveConnection.
    4. Match the  Antecedent attribute of Msvm_ActiveConnection with the PATH of SwitchPort
    5. When there is a match, find a VM whose GUID is listed in the Dependent attribute of Msvm_ActiveConnection
    Note

    The script finds all virtual machines that are attached to a given virtual machines. Those virtual machines must be running ,.ie. powered on to be listed by the script.

    Script

    $ListofVMs = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName <> Name"
    $ListofSwitches = gwmi -namespace root\virtualization Msvm_VirtualSwitch
    $ListofSwitchPorts = gwmi -namespace root\virtualization Msvm_SwitchPort

    foreach ($Switch in $ListofSwitches)
    {
        $SwitchGUID = $Switch.Name
        $SwitchDisplayName = $Switch.ElementName
        $PortsOnSwitch = $ListofSwitchPorts | where {$_.SystemName -match $SwitchGUID}

        foreach ($Port in $PortsOnSwitch)
        {
            $PortPath = $Port.__PATH
            $ListofConnections = gwmi -namespace root\virtualization Msvm_ActiveConnection
            $a = $ListofConnections | where {$_.Antecedent -like $PortPath}
            if ($a -ne $NULL)
            {
                $LANEndPoint = $a.Dependent
                foreach ($VM in $ListofVMs)
                {
                    $VMGUID = $VM.Name
                    $VMDisplayName = $VM.ElementName
                    if ($LanEndPoint -like "*$VMGUID*")
                    {
                        write-host "VM --> $VMDisplayName is connected to Switch --> $SwitchDisplayName"
                    }
                }
            }
        }
    }

    Enjoy!

    /Dung

    HyperV WMI PowerShell

    10 juni

    Hyper-V WMI Explained - Part II

     

    Management Services

    In Hyper-V there are 3 services that govern all management activities of the virtual environment:

    • Hyper-V Virtual Machine Management Service (vmms) - This service is used to control creation, deletion and  modification of virtual machines. It also provides mechanisms to perform operations on virtual machines such as taking snapshots, importing and exporting virtual machines. in Hyper-V WMI, the service is represented by the class Msvm_VirtualSystemManagementService. If you want to take actions on virtual machines, you must first obtain an instance of this class by querying WMI and use one of its public methods.
      Example : $VM_Service = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemManagementService

     

    • Hyper-V Networking Management Service (nvspwmi) - This service is used to control creation, deletion of networking resources such as virtual switches, switch ports and internal Ethernet ports.  in Hyper-V WMI, the service is represented by the class Msvm_VirtualSwitchManagementService.
      Note:  the service does not manage network adapters (NICs) of virtual machines. NICs are resources of virtual machines so you need to use the Virtual Management Service.

     

    • Hyper-V Image Management Service (virtsvcs) - This service manages virtual media (.vhd, .vfd) for virtual machines..  in Hyper-V WMI, the service is represented by the class Msvm_ImageManagementService.

    The figure below maps various action items shown in the Hyper-V Manager console to the three services described above

    Hyper-V-Services

    Until the next time!

    Enjoy,

    /Dung

    HyperV WMI PowerShell

    Hyper-V WMI Explained - Part I

     

    Introduction

    I received many requests to explain some PowerShell examples on Hyper-V WMI so now it's great time to start a new series to dive in more detailed explanation about Hyper-V WMI.

    First let me say that your support through e-mail in the last few months are exceptional and I'd like to thank all the folks who send comments/suggestions to enhance the examples ( except for some scams in the blog!). Second let me write down a short disclaimer before going into technical details.

     

    Disclaimer

    All the information provided here are based on my own understanding of Hyper-V WMI during many hours of trials and tests. I use the two documents to learn about Hyper-V WMI:

    1. Hyper-V WMI documentation. The MSDN library contains Beta2 documentation of WMI.  Even though some classes/methods/properties are changed with the current release (Hyper-V RC1), most of them are still valid.
      Note:  I hope that MS will update the library ASAP when Hyper-V hits RTM
    2. DMTF CIM System Virtualization model: This is THE doc that you need to read/consult frequently if you are serious about using WMI and Hyper-V to build management tools. I know that MS is defining/pushing/using standards in the management space, so no surprise that they are using the virtualization model defined by the DMTF task force.

    In addition, I extensively use PowerShell and especially the get-member cmdlet to discover methods and properties of WMI object classes. This is a must if you want to write scripts or code against Hyper-V WMI. There certainly are other tools to browse WMI objects and I find Powershell quite handy for me.

    Finally, explanations provided are my interpretation of the documentation and based on my own testing. As such all the errors are also mine too!

    Now let's start then!

    /Dung

    HyperV WMI PowerShell

    06 juni

    Hyper-V WMI Examples - Part XVII

     

    Removing virtual machines from Hyper-V

    The following script is used to remove virtual machines from the Hyper-V console.

    ##    Connect to the Virtual Management Service
    ##
    $Server = 'localhost'
    $VM_Service = get-wmiobject -computername $server -namespace root\virtualization Msvm_VirtualSystemManagementService
    ##
    ## Remove "01_xxxx"Vms from the console
    ##
    $ListofVMs = get-wmiobject -computername $server -namespace root\virtualization Msvm_ComputerSystem | where {$_.ElementName -like '*01*'}
    foreach ($VM in $ListofVMs)
    {
        if ($VM -ne $Null)
        {
        $VM_Service.DestroyVirtualSystem($VM.__PATH)
        }
    }

     

    Enjoy!

    /Dung

    HyperV WMI PowerShell

    03 juni

    Hyper-V WMI Examples - Part XVI

     

    Changing the Boot order of a VM

    Sometimes you may want to change the Boot order of a VM, for instance, to PXE boot a VM, to boot from a CD/DVD, or to simply fix the order to always start booting from the disk.

    You use the Msvm_VirtualSystemSettingData class to change the boot order setting f a VM. Objects of this class represents virtual "motherboard"" of a virtual machine and store virtualization-specific settings of a VM.

     

    Script

    $VMName = "My Virtual Machine"

    $VM_Service = get-wmiobject –namespace root\virtualization Msvm_VirtualSystemManagementService

    # Step 1

    $VM = get-wmiobject –namespace \root\virtualization Msvm_ComputerSystem | where {$_.ElementName -like $VMName )

    # Step 2 - Get its "motherboard"

    $MB = get-wmiobject –namespace \root\virtualization Msvm_VirtualSystemSettingData | where {$_.ElementName -like $VMName )

    # Step 3 - Change the Boot Order

    # Values are 0: Boot from floppy – 1: Boot from CD – 2: Boot from disk – 3:PXE Boot

    $MB.BootOrder = 3,1,2,0

    # Step 4

    $VM_Service.ModifyVirtualSystem($VM.__PATH, $MB.psbase.GetText(1))

     

    Et Voilà!

    Enjoy!

    /Dung

    HyperV WMI PowerShell

     
    *