VMware Cloud Community
nikpetrov95
Contributor
Contributor
Jump to solution

Create DRS rules for groups VM

Some VMs have a custom attribute that specifies cluster nodes, I need to merge those nodes into one drs rule and make sure they are separated from each other.

Here is an example

$Nodes = VM1,VM2,VM3,VM4|Get-Annotation -CustomAttribute "Nodes"|?{$_.Value -ne ""}|select Value -Unique

$Nodes

Value
-----
VM1;VM3
VM2;VM4



After collecting these attributes, I do not understand what to do next, I try to separate values, but nothing comes out

foreach ($Node in $Nodes){

get-vm ($Node.value -replace ";", ",")

}


Get-VM VM with name 'VM1;VM3' was not found using the specified filter(s).

@LucDCan you help me?

Ideally, check for rules with current VMs and skip if they exist

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Do you mean like this?

Get-VM | Get-Annotation -CustomAttribute Nodes |
    Where-Object { $_.Value -ne '' } |
    Select-Object -Property Value -Unique |
ForEach-Object -Process {
    $vmNames = $_.Value.Split(';')
    New-DrsRule -Cluster 'cluster' -Name "Rule-$($vmNames -join '-')" -VM $vmNames -KeepTogether:$false
}


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure how these Custom Attributes are related to DRS VM Groups?
Can you elaborate?


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

0 Kudos
nikpetrov95
Contributor
Contributor
Jump to solution

For example, there are 2 VMs with a database

VM1 - Primary
VM2 - Secondary

For each VM in the attributes, we indicate all members of the cluster database

nikpetrov95_0-1669974353449.png

Based on these data, I need to make a separate drs rule

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean something like this?

$vm = Get-VM | Get-Annotation -CustomAttribute Nodes |
    Where-Object { $_.Value -ne '' } |
    Select-Object -ExpandProperty AnnotatedEntity

New-DrsVMGroup -Cluster 'cluster' -Name 'Groupname' -VM $vm


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

0 Kudos
nikpetrov95
Contributor
Contributor
Jump to solution

It works if all VMs are added to one rule.

But here the task is a little different.

There is a VM

VM1 value VM1; VM2
VM2 value VM1; VM2
VM3 value VM3; VM4
VM4 value VM3; VM4

So I need to make 2 different rules

for VM1; VM2 and VM3; VM4

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not following.
You need 2 VM groups, but based on what?
Do they have different values in the Custom Attribute?


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

0 Kudos
nikpetrov95
Contributor
Contributor
Jump to solution

I have 100 VMs for every 2 VMs (there are also clusters of 3 VMs) the same attribute, 50 rules need to be made for these 100 VMs

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Still not following I'm afraid.

How can the script know which 2 (or 3) VMs need to go in 1 group, if they all have the same Custom Attribute?
Or are there multiple clusters, with each having 2 or 3 VMs?


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

0 Kudos
nikpetrov95
Contributor
Contributor
Jump to solution

As a first step, I selected all the unique values of the NODES attribute

$Nodes = get-vm|Get-Annotation -CustomAttribute "Nodes"|?{$_.Value -ne ""}|select Value -Unique

Next, I want to take each value in turn and split it into VMs and create a rule.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean like this?

Get-VM | Get-Annotation -CustomAttribute Nodes |
    Where-Object { $_.Value -ne '' } |
    Select-Object -Property Value -Unique |
ForEach-Object -Process {
    $vmNames = $_.Value.Split(';')
    New-DrsRule -Cluster 'cluster' -Name "Rule-$($vmNames -join '-')" -VM $vmNames -KeepTogether:$false
}


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