14
CHAPTER 2 | Software-defined datacenter
Figure 2-2:
VM Groups
The code that follows is a Windows PowerShell script that will accomplish our goals. Keep
in mind that
the VM group being created is a VM collection group. Only VM collection groups can have VMs
directly placed within them.
#Setup VM variables
$VM1 = Get-VM -Name VM1
$VM2 = Get-VM -Name VM2
$VM3 = Get-VM -Name VM3
#Create new VM Group
New-VMGroup -Name TestVMG1 -GroupType VMCollectionType
#Setup VM Group variable
$TestVMG1 = Get-VMGroup -Name TestVMG1
#Add VMs to the group/collection
Add-VMGroupMember -VMGroup $TestVMG1 -VM $VM1
Add-VMGroupMember -VMGroup $TestVMG1 -VM $VM2
Add-VMGroupMember -VMGroup $TestVMG1 -VM $VM3
The result of these steps is a VM group that contains three VMs.
You can verify this by using the management tools and querying either the VMs or the VM groups.
The following example shows how to do this by utilizing the Get-VM and Get-VMGroup cmdlets,
respectively:
PS C:\> Get-VM | ft Name, state, groups - AutoSize
Name
State Groups
---- ----- ------
VM1 Running {TestVMG1}
VM2 Running {TestVMG1}
VM3 Running {TestVMG1}
PS C:\> Get-VMGroup * | ft Name, vmmembers -AutoSize
Name VMMembers
---- ---------
TestVMG1 {VM2, VM3, VM1}
The updated Get-VM cmdlet lists what groups (if any) of which the VM is a member. A VM can be a
member of multiple groups. If this is
the case, the Get-VM cmdlet will return a list of multiple groups.
The new Get-VMGroup lists any VMs that are members of a specified group, or, as in the preceding
example, in which we use a wildcard, all existing groups. In the example, we
query all groups because
we know there is just one. However, we can add one of the VMs to the membership of second group.
Here is a quick Windows PowerShell script that will do just that:
#Create new VM Group
New-VMGroup -Name TestVMG2 -GroupType VMCollectionType
#Setup VM Group variable
$TestVMG2 = Get-VMGroup -Name TestVMG2
#Add VMs to the group
Add-VMGroupMember -VMGroup $TestVMG2 -VM $VM1
15
CHAPTER 2 | Software-defined datacenter
Using the Get-VM cmdlet, you can see that VM1 now belongs to both the TestVMG1 group and the
new TestVMG2 group:
PS C:\> Get-VM | ft Name, state, groups - AutoSize
Name State Groups
---- ----- ------
VM1 Running {TestVMG2, TestVMG1}
VM2 Running {TestVMG1}
VM3 Running {TestVMG1}
Using the Get-VMGroup cmdlet, you now see both groups and VM1 are members of both VM groups:
PS C:\> Get-VMGroup * | ft Name, vmmembers -AutoSize
Name VMMembers
---- ---------
TestVMG2 {VM1}
TestVMG1 {VM2, VM3, VM1}
There are now two VM groups: one
comprising three VMs, and the other with a single VM, as shown
in Figure 2-3.
Do'stlaringiz bilan baham: