Hi - I was hoping someone could assist in writing the required powershell code to remove a VM from a DRSGroup.
So far, I have (which is based on Add-VMToDrsGroup from (http://poshcode.com/4118)
$Cluster = Get-Cluster -Name "MainCluster"
$DrsGroup = "PrimaryGroup"
$DrsGroup = Get-DrsGroup -Cluster $Cluster -Name $DrsGroup
$vm = get-vm vm1-3
$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 = "remove"
$spec.groupSpec[0].info = $DrsGroup
$spec.groupSpec[0].RemoveKey = $VM.ExtensionData.MoRef
$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
I have a VM DRSGroup called PrimaryGroup and its has the machine named VM1-3 within it.
Source for Get-DRSGroup (http://poshcode.com/4118)
The result of running the above is that in the vSphere client i see, "The specified parameter was incorrect" . Can anyone assist in identifying the incorrect parameter?
thanks in advance.
I normally do this as follows
$Cluster = Get-Cluster -Name "MainCluster"
$DrsGroupName = "PrimaryGroup"
$DrsGroup = Get-DrsGroup -Cluster $Cluster -Name $DrsGroupName
$vm = get-vm vm1-3
$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 = $DrsGroupName
$spec.groupSpec[0].Info.Vm = ($cluster.ExtensionData.ConfigurationEx.Group |
where {$_.Name -eq $DrsGroupName}).VM | where {$_ -ne $vm.ExtensionData.MoRef}
$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I normally do this as follows
$Cluster = Get-Cluster -Name "MainCluster"
$DrsGroupName = "PrimaryGroup"
$DrsGroup = Get-DrsGroup -Cluster $Cluster -Name $DrsGroupName
$vm = get-vm vm1-3
$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 = $DrsGroupName
$spec.groupSpec[0].Info.Vm = ($cluster.ExtensionData.ConfigurationEx.Group |
where {$_.Name -eq $DrsGroupName}).VM | where {$_ -ne $vm.ExtensionData.MoRef}
$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, thats perfect!
Is it possible to achieve the same using RemoveKey? I ask purely to increase my knowledge in using the API - I spent a little while looking around the sdk trying to understand how to do it, but clearly I was unsuccessful.
For the benefit of others, the script requires the following definition
$DrsGroupName = $DrsGroup
I'm afraid I never got that RemoveKey to work.
Btw, I updated the code to say $DrsGroupName
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
With the help of Onyx I found out when the RemoveKey property is used.
That property must hold the groupname when you want to remove the complete group.
$Cluster = Get-Cluster -Name "Cluster"
$DrsGroupName = "Group"
$DrsGroup = Get-DrsGroup -Cluster $Cluster -Name $DrsGroupName
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$group = New-Object VMware.Vim.ClusterGroupSpec
$group = New-Object VMware.Vim.ClusterGroupSpec
$group.operation = "remove"
$group.info = $DrsGroup
$group.RemoveKey = $DrsGroupName
$spec.GroupSpec += $group
$Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference