VMware Cloud Community
worm_za
Contributor
Contributor

Getting List of VM's of a Host Affinity Rule

Hi Everyone,


I'm trying to get a list of VM's that are part of a specific Host Affinity Rule via PowerCli (or any easier way if anyone knows of one).

I run the command "Get-DrsRule -Type VMHostaffinity -Cluster CLUSTERNAME and get the following Results:

Name                                Enabled Type            VMIDs

----                                ------- ----            -----

AffinityRule1                       True    VMHostAffinity  {VirtualMachine-vm-5716}

AffinityRule2                       True    VMHostAffinity  {VirtualMachine-vm-5872}

AffinityRule3                       True    VMHostAffinity  {VirtualMachine-vm-623, VirtualMachine-vm-4726,

I've seen some posts on how to convert the VMID to a VMNAME, but I can't seem to get them to work. They all seem related to VM Affinity rules.

I was looking at the VI-Property, but can't find the correct -ObjectType to use for Host Affinity Rules. Smiley Sad

Added to that, if it is possible, I'd also like to pull a list of hosts that are part of that specific Affinity Rule.

Any help is appreciated

Tags (2)
12 Replies
UmeshAhuja
Commander
Commander

Hi,

-------------- Example 1 --------------

$myCluster = Get-Cluster -Name "MyCluster1" Get-DrsRule -Cluster $myCluster -Name "*Rule1*"

Retrieves the DRS rules for the cluster stored in the $myCluster variable, whose names contain "Rule1".

-------------- Example 2 --------------

Get-Cluster -Name 'MyCluster1' | Get-DrsRule

Retrieves the virtual machine affinity and anti-affinity rules for the specified cluster by pipeline.

-------------- Example 3 --------------

$myVm1 = Get-VM -Name 'MyVm1' $myCluster1 = Get-Cluster 'MyCluster1' Get-DrsRule -Cluster $myCluster1 -VM $myVm1

Retrieves the virtual machine affinity and anti-affinity rules for the specified virtual machine in the specified cluster.

-------------- Example 4 --------------

Get-Cluster 'MyCluster1' | Get-DrsRule -Type VMHostAffinity

Retrieves virtual machine to host affinity rules for the specified cluster by pipeline.

-------------- Example 5 --------------

$myVMHost1 = Get-VMHost -Name 'MyVMHost1' $myCluster1 = Get-Cluster -Name 'MyCluster1' Get-DrsRule -Cluster $myCluster1 -VMHost $myVMHost1

Retrieves virtual machine to host affinity rules for the specified host and cluster.

Thanks n Regards
Umesh Ahuja

If your query resolved then please consider awarding points by correct or helpful marking.
worm_za
Contributor
Contributor

Hi Umesh,

I'm not really getting anywhere with the Examples.

I was hoping for a bit more guidance based on the information I've supplied. As you can see I sort of get the results by using the info in Example 4. I get the AffinityRule Name, and the list of VM's in the rules, but it is not in the correct format. It gives the VMID, and I would like the VMName.

I'm quite new to PowerCli, so need as much guidance as possible. :smileyblush:

Thanks.

0 Kudos
LucD
Leadership
Leadership

mattboren and myself made a module for DRS rulles, that should make working with them a lot simpler.

It makes querying and creating host and VM groups quite easy.

See DRSRule – a DRS rules and groups module


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

75109
Contributor
Contributor

Get-DrsRule -Cluster $cluster | select Cluster,Name,KeepTogether,Enabled, @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name}}}

likeahoss
Enthusiast
Enthusiast

That's the ticket.

0 Kudos
sushilkm
Enthusiast
Enthusiast

LucD​  mattboren

Looks like the below code is outdated for latest version of powercli

https://github.com/PowerCLIGoodies/DRSRule/archive/Latest.zip

Getting below error

PS C:\PS1\DRSRule-Latest>

PS C:\PS1\DRSRule-Latest> Get-DrsVMGroup

Unable to find type [DRSRule.VMGroup].

At C:\Program Files\WindowsPowerShell\Modules\DRSRule\1.2.1\DRSRule.psm1:55 char:15

+   [OutputType([DRSRule.VMGroup],[VMware.Vim.ClusterVmGroup])]

+               ~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (DRSRule.VMGroup:TypeName) [], RuntimeException

    + FullyQualifiedErrorId : TypeNotFound

PS C:\PS1\DRSRule-Latest> Get-DrsVMHostGroup

Unable to find type [DRSRule.VMHostGroup].

At C:\Program Files\WindowsPowerShell\Modules\DRSRule\1.2.1\DRSRule.psm1:167 char:15

+   [OutputType([DRSRule.VMHostGroup],[VMware.Vim.ClusterHostGroup])]

+               ~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (DRSRule.VMHostGroup:TypeName) [], RuntimeException

    + FullyQualifiedErrorId : TypeNotFound

PS C:\PS1\DRSRule-Latest>

PS C:\PS1\DRSRule-Latest>

PS C:\PS1\DRSRule-Latest> $PSVersionTable

Name                           Value

----                           -----

PSVersion                      5.1.14409.1012

PSEdition                      Desktop

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

BuildVersion                   10.0.14409.1012

CLRVersion                     4.0.30319.42000

WSManStackVersion              3.0

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

PS C:\PS1\DRSRule-Latest>

0 Kudos
LucD
Leadership
Leadership

That issue (see Metadata file VMware.Vim.dll could not be found · Issue #15 · PowerCLIGoodies/DRSRule · GitHub ) should be fixed.

Are you using DRSRule 1.2.1?


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

sushilkm
Enthusiast
Enthusiast

Not sure of DRS rule version. could not find any version info. Do i need to use some other version?

I downloaded the latest one from link . It;s a zip file.

https://codeload.github.com/PowerCLIGoodies/DRSRule/zip/Latest

0 Kudos
sushilkm
Enthusiast
Enthusiast

Looking back into my powerCLi directory, I am usning DRS 1.2.1

0 Kudos
LucD
Leadership
Leadership

You can do

Get-Module -Name DRSRule -ListAvailable

or look in the .psd1 file.


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

0 Kudos
LucD
Leadership
Leadership

It should be fixed in 1.2.1, unless you are seeing a new issue.

Let me check.


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

sushilkm
Enthusiast
Enthusiast

Seems like a New issue.

PS C:\PS1\DRSRule-Latest> Get-Module -Name DRSRule -ListAvailable

Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands

---------- -------    ----                                ----------------

Script     1.2.1      DRSRule                             {Get-DrsVMGroup, Get-DrsVMHostGroup, Get-DrsVMToVMHostRule, Get-DrsVMToVMRule...}

PS C:\PS1\DRSRule-Latest>

0 Kudos