I am looking for a way to append a DRS Group and coming up a few issues. I am starting with this code below to add a virtual machine.
# ------- ReconfigureComputeResource_Task -------
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
$spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[0].operation = "Edit"
$spec.groupSpec[0].info = New-Object VMware.Vim.ClusterVmGroup
$spec.groupSpec[0].info.name = "GroupedVM"
$spec.groupSpec[0].info.vm = New-Object VMware.Vim.ManagedObjectReference[] (2)
$spec.groupSpec[0].info.vm[0] = New-Object VMware.Vim.ManagedObjectReference
$spec.groupSpec[0].info.vm[0].type = "VirtualMachine"
$spec.groupSpec[0].info.vm[0].Value = "vm-2175"
$_this = Get-View -Id 'ClusterComputeResource-domain-c7'
$_this.ReconfigureComputeResource_Task($spec, $true)
Each time I run this script it overwrites any all virtual machines that are a member of this DRS group and then adds the one in the script.
Is there any thing I can do to append a DRS Group? Any Help would be greatly appreciated.
Take a look at the Add-VMToDrsGroup function from http://communities.vmware.com/message/1699823#1699823.
Regards, Robert
Thanks for the information.
I have looked at this script and have tried running it in the lab and doesn't seem to do anything for me. Do you happen to have an example of how to run this script. (I look at the comments in the script but nothing seems to be happening when I run it with my values)
Thank you
B
An example of the function call is:
Add-VMToDrsGroup -Cluster MyCluster -DrsGroup MyVMDrsGroup -VM MyVm
The Add-VMToDrsGroup function uses the Get-DrsGroup function. So you have to define them both.
You can also do:
Add-VMToDrsGroup -Cluster (Get-Cluster MyCluster) -DrsGroup (Get-DrsGroup -Cluster MyCluster -Name MyVMDrsGroup) -VM (Get-VM MyVM)
I tested the function with success against a vSphere 4.1 and 5.0 cluster.
The Add-VMToDrsGroup function creates a task. Do you wait untill the task has completed before you check the results?
Message was edited by: RvdNieuwendijk Added the sentence about the task.
That's because you use the "edit" operation.
$spec.groupSpec[0].operation = "edit"
The ArrayUpdateOperation enum has 3 values. To add a VM to the group you need to use "add".
$spec.groupSpec[0].operation = "add"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So here is what I have found.
The $spec.groupSpec[0].operation = "Add" doesn't seem to work eventhough you would think it would.
Since the servers that I need to add to the DrsVMGroup start with the same name I created this to just recreate the entire list.
$VM = Get-VM -Name xxxx*$c = 0
# ------- ReconfigureComputeResource_Task -------
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
$spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[0].operation = "Edit"
$spec.groupSpec[0].info = New-Object VMware.Vim.ClusterVmGroup
$spec.groupSpec[0].info.name = "GroupedVM"
$spec.groupSpec[0].info.vm = New-Object VMware.Vim.ManagedObjectReference[] ($VM.count)foreach ($1VM in $VM)
{
$vmid = $1VM.Id
$vmid = $vmid -replace("VirtualMachine-","")$spec.groupSpec[0].info.vm[$c] = New-Object VMware.Vim.ManagedObjectReference
$spec.groupSpec[0].info.vm[$c].type = "VirtualMachine"
$spec.groupSpec[0].info.vm[$c].Value = $vmid$c = $c + 1
}$_this = Get-View -Id 'ClusterComputeResource-domain-c7'
$_this.ReconfigureComputeResource_Task($spec, $true)
Not the most elegant solution but seems to work for me. I would guess if a person wanted to replace the first line with some code to pull the active list of servers assign to that DRS Group and set it up as an array,