VMware Cloud Community
Greenwich789
Contributor
Contributor
Jump to solution

Remove VM from DRSGroup

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.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Greenwich789
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos