VMware Cloud Community
svillar
Enthusiast
Enthusiast

Create a dynamic variable based on host name

Hi,

I want to create a "return to home" set of DRS affinity rules so that any time I want the VMs to go back to where they started from.  To start, I did this: 

$esx01vms = get-vmhost ESX01* | get-vm
$esx02vms = get-vmhost ESX02* | get-vm

etc.  I would then use them in a

Get-DrsClusterGroup "MyClusterGroup" | Set-DrsClusterGroup -VM $vm -Add with the name also referring to the ESXi hostname.

There must be a way to create these variables dynamically.  I would need to split at the .domainname.com, and a foreach loop of some kind.

Any help would be appreciated.

0 Kudos
7 Replies
LucD
Leadership
Leadership

Not sure what you mean by " I would need to split at the .domainname.com".
Do you only want to use the 1st qualifier of the FQDN of the ESXi nodes?

I set the new DRS rules to Disabled, you would need to change that when required.
And of course disable all conflicting rules you might have.

Try something like this.
Note, this will run against all your clusters like this.
Use the Name parameter on the Get-Cluster cmdlet if you want to limit the scope.

 

Get-Cluster -PipelineVariable cluster |
Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  $esxQ1 = $esx.Name.Split('.')[0]
  $vms = Get-VM -Location $esx
  $vmGroupName = "DRS-VM-$esxQ1"
  $vmhostGroupName = "DRS-VMHost-$esxQ1"
  $ruleName = "DRS-$esxQ1"

  Get-DrsClusterGroup -Cluster $cluster -Name $vmGroupName -ErrorAction SilentlyContinue |
  Remove-DrsClusterGroup -Confirm:$false
  $vmGroup = New-DrsClusterGroup -Cluster $cluster -Name $vmGroupName -VM $vms

  Get-DrsClusterGroup -Cluster $cluster -Name $vmhostGroupName -ErrorAction SilentlyContinue |
  Remove-DrsClusterGroup -Confirm:$false
  $vmHostGroup = New-DrsClusterGroup -Cluster $cluster -Name $vmhostGroupName -VMHost $esx

  Get-DrsVMHostRule -Cluster $cluster -Name $ruleName -ErrorAction SilentlyContinue |
  Remove-DrsVMHostRule -Confirm:$false
  New-DrsVMHostRule -Cluster $cluster -Name $ruleName -Type MustRunOn -VMGroup $vmGroup -VMHostGroup $vmhostGroup -Enabled $false
}

 

 


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

0 Kudos
svillar
Enthusiast
Enthusiast

Yes, all I wanted was the hostname, without FQDN.  Thanks.

While I appreciate that you created VMgroup, the Hostgroup and the rule, I'm not sure that my question was answered.  If you define the variables based on the text, how does it change dynamically?  Unless this is part of "-PipeLineVariable" ?  What does that part do?

How is that variable "$esxQ1"  used?  Something more than just storing the hostname, right?  You would have used $esx, then, no? 

Thanks so much!

0 Kudos
LucD
Leadership
Leadership

The $esxQ1 variable is used to store the 1st qualifier of the FQDN of the ESXi hostname.

I'm not sure what you mean by "how does it change dynamically".
Do you actually expect the DRS rule groups to change dynamically?
I'm afraid that is not possible.
You should run this script periodically, which can be done via a scheduled task.


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

0 Kudos
svillar
Enthusiast
Enthusiast

So my question is actually not about creating the rule.  It's about a variable for each ESXi host, without manually doing it.

I want to loop through all my hosts creating (and then referencing later) the $esx## variable.  I can do this manually for each host, such as

$esx01 = get-vmhost host-esx01.*
$esx02 = get-vmhost host-esx02*

.....

But isn't there a way to do something like

$esx## = "go through my hosts and create a variable based on the name of the host, or (in this case) the last 5 characters of the name of the host (esx##.domain.com)"

Thanks again!

 

 

0 Kudos
LucD
Leadership
Leadership

If I understand you correctly, you want a PowerCLI script without running a script.

Then you should probably look at other products like Aria.


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

0 Kudos
svillar
Enthusiast
Enthusiast

This is not at all what I'm asking for.  Tell me this:
Does $esxQ1 change for every host?  Is that what you meant by 

"The $esxQ1 variable is used to store the 1st qualifier of the FQDN of the ESXi hostname."  ?

If so, this is exactly what I'm looking for and I didn't understand it.  I think the Q1 part threw me off and that it referred only to ESX01.

This is why I asked what -PipelineVariable means.  Does "Get-VMHost -PipelineVariable esx" create the $esx variable?

In short, if this loops through all of my hosts, pulls the vms on each, and creates a rule based on the name of the host, I'm good.

 

Finally, in the last line -    -Type MustRunOn, is the other option ShouldRunOn?

 

0 Kudos
LucD
Leadership
Leadership

Yes, with the Split method the FQDN is split in all the qualifiers in an array.
The [0] index takes the 1st qualifier.

The PipelineVariable parameter stores the pipeline object (which is $_) in a named variable, $esx in this case.
This allows for later reference to the object via that variable.

And yes, the snippet loops through all the ESXi nodes in a cluster, and then creates the group and finally the rule.

Note that the outer loop goes through all clusters.
If you want to limit the snippet to only 1 cluster, use the Name parameter.

Get-Cluster -Name MyCluster |


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

0 Kudos