VMware Cloud Community
jitla1971
Enthusiast
Enthusiast
Jump to solution

Powercli Script To Move VM's from 1 Virtual Machine DRS Group To Another Virtual Machine DRS Group and then back again.

Hello Group

I am looking for a powercli script to add vms to a Virtual Machine DRS group, without the need to manually add each vm to the group.

Not sure if this is possible or what is required, so any help would be greatly appreciated.

Kind Regards

Jitla

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could split up the logic in 2 scripts, and save the VM names in a file.

Something like this

Before the maintenance

$fileName = '.\group1-vm-names.csv'

$clusterName = 'Metro Production'

$cluster = Get-Cluster -Name $clusterName

$drsGroup1 = Get-DrsClusterGroup -Name Slough -Cluster $cluster

$drsGroup2 = Get-DrsClusterGroup -Name Langley -Cluster $cluster

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $drsGroup1.Member -Add

Set-DrsClusterGroup -DrsClusterGroup $drsGroup1 -VM $drsGroup1.Member -Remove

$drsGroup1.Member | Select Name | Export-Csv -Path $fileName -NoTypeInformation -UseCulture

And then after the maintenance

# After the maintenance

$fileName = '.\group1-vm-names.csv'

$clusterName = 'Metro Production'

$cluster = Get-Cluster -Name $clusterName

$drsGroup1 = Get-DrsClusterGroup -Name Slough -Cluster $cluster

$drsGroup2 = Get-DrsClusterGroup -Name Langley -Cluster $cluster

$group1VM = Get-VM -Name (Import-Csv -Path $fileName -UseCulture).Name

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $group1VM -Remove

Set-DrsClusterGroup -DrsClusterGroup $drsGroup1 -VM $group1VM -Add


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

With New-DrsClusterGroup you can add multiple VMs in 1 call to a group.

The VM parameter accepts an array of VMs.

Or do you mean something else?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jitla1971
Enthusiast
Enthusiast
Jump to solution

Hi Luc

Apologies I dont think i explained myself correctly.

Scenario, we have 2 sites where our esxi hosts are located, Slough and Langley.

Each site has a VM DRS Group, which has VM;s for that site.

Example Slough VMs DRS Group contains VMs located in Slough only.

               Langley VM DRS Group contains VM's located in Langley only.

Due to planned maintenance we need to power down all the hosts located in Slough.

So what we want to do, is use powercli if possible to move all the vms contained in the Slough VM DRS Group to the Langley VM DRS Group.

Once we have finished our maintenance, we want to use a script to move vms which we added to the  Langley VM DRS Group back to the Slough VM DRS Group.

Rather than do this manually, we thought we could try automating the process.

I hope makes things more clear.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

$drsGroup1 = Get-DrsClusterGroup -Name Slough

$drsGroup2 = Get-DrsClusterGroup -Name Langley

# Before the maintenance

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $drsGroup1.Member -Add

# After the maintenance

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $drsGroup1.Member -Remove


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jitla1971
Enthusiast
Enthusiast
Jump to solution

Hi Luc

So the line Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $drsGroup1.Member -Add

Will move virtual machines which are part of the slough Virtual Machine DRS Group to the Langley Virtual Machine DRS Group, if that is the case, then yes that is what we need.

When removing the virtual machines the Langley Virtual Machine DRS group, how will it know which Virtual machines to move to the Slough Virtual Machine DRS Group?

As the Langley Virtual Machine Group already contains virtual machines residing on Langley hosts.

Also forgot to state we have several clusters within our vcenter, so we only want to run this script against a specific cluster.

example we want to run script in our Metro Production cluster.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could split up the logic in 2 scripts, and save the VM names in a file.

Something like this

Before the maintenance

$fileName = '.\group1-vm-names.csv'

$clusterName = 'Metro Production'

$cluster = Get-Cluster -Name $clusterName

$drsGroup1 = Get-DrsClusterGroup -Name Slough -Cluster $cluster

$drsGroup2 = Get-DrsClusterGroup -Name Langley -Cluster $cluster

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $drsGroup1.Member -Add

Set-DrsClusterGroup -DrsClusterGroup $drsGroup1 -VM $drsGroup1.Member -Remove

$drsGroup1.Member | Select Name | Export-Csv -Path $fileName -NoTypeInformation -UseCulture

And then after the maintenance

# After the maintenance

$fileName = '.\group1-vm-names.csv'

$clusterName = 'Metro Production'

$cluster = Get-Cluster -Name $clusterName

$drsGroup1 = Get-DrsClusterGroup -Name Slough -Cluster $cluster

$drsGroup2 = Get-DrsClusterGroup -Name Langley -Cluster $cluster

$group1VM = Get-VM -Name (Import-Csv -Path $fileName -UseCulture).Name

Set-DrsClusterGroup -DrsClusterGroup $drsGroup2 -VM $group1VM -Remove

Set-DrsClusterGroup -DrsClusterGroup $drsGroup1 -VM $group1VM -Add


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jitla1971
Enthusiast
Enthusiast
Jump to solution

Thank you very much Luc that would be perfect, your help has really been appreciated.

$filename = are the quotes single when stating the file path?

would something like this work

$filename = 'd:\sloughvms.csv' or does it need to be $filename = "d:\sloughvms.csv"

lastly what version of powercli would I need to use this script? currently, we have powercli 6.3 release 1 installed, I am assuming a fresh installation of the latest version is required.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The DRS cmdlets were added in 6.5.1, so yes, an upgrade would be required.

I would suggest to go for the latest PowerCLI 10, which is available on the PowerShell Gallery.

The single or double quotes only make a difference when you would use a variable in there.

With single quotes there is no variable substitution, with double quotes there is.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jitla1971
Enthusiast
Enthusiast
Jump to solution

sorry for the additional questions Luc, but will this work on a ESXi 6 host or is it only for ESXi 6.5 hosts?

how do i access the powershell gallery?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

On the new PowerCLI delivery method see Welcome PowerCLI to the PowerShell Gallery – Install Process Updates

and Updating PowerCLI through the PowerShell Gallery

No, this will work for vSphere 6 as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jitla1971
Enthusiast
Enthusiast
Jump to solution

Hi Luc

Just wanted to give you an update.

Got powercli working via the Poweshell Gallery, so thank you.

My manager has advised, we will be moving vms to different vm drs group in batches of 10, is there a script that will do this.

As a test i used the following command to move the vm to a different drs group

Set-DrsClusterGroup -DrsClusterGroup "TestLangleyDRSVMs" -VM Prod-APPVM-D1 -Add

Only issue i found, was that the vm was still listed in the original virtual machine drs group as well as the new virtual machine drs group, i moved the vm to.

I would have thought, once we add vm to new drs group, it would remove it from original but that is not the case, so had to run this command on original drs group

Set-DrsClusterGroup -DrsClusterGroup "TestSloughDRSVMs" -VM Prod-APPVM-D1 -Remove.

Is there a way to combine commands, so that once you add to new drs group it will remove from original drs group?

Your help has been really appreciated, thank you very much.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, you have to explicitly remove the VMs from the other group (the -Remove option, see code above).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Mikhalich93
Contributor
Contributor
Jump to solution

Thank you so much for the nice code!

Reply
0 Kudos