VMware Cloud Community
marklemon
Enthusiast
Enthusiast
Jump to solution

DRS Rule for Host Affinity checking of datatore names that they match hosts

DRSRules - List of Host Affinity Rules

Host Affinity Rules

 

Cluster

Enabled

Name

Keep Together

VM

Rule Host

Running on

Cluster-SA1

TrueCluster2-SA1-VMsHostsFalseVMs-SA1SA1sa1_host3

Cluster-SB2

True

Cluster1-SB2-VMsHosts

False

VMs-SB2

SB2

sb2_host2

This module of vCheck will show me if any of my VMs aren't running according to the ruleset, but I want to look deeper. 

DRS Rule for Host Affinity checking of datatore names that they match hosts

Our hosts all start with three characters to differentiate between SiteA1 and SiteB1, i.e. sa1host1 sa1host2..sa1host9, and sb2host1, sb2host2, sb2host9, etc.

Our datastores are names “sa1_exch01”, “sa1_sql_01”, sa1_data_01” for all the datastores that are located on Site1.  While datastores named “sb2_exch01”, “sb2_sql_01”, sb2_data_01” for all the datastores that are located on Site2.

Can you help with a script that will check all the VMs on hosts on Site1 and report any that have datastores that don’t start with “sa1” and similarly any VMs on hosts on Site2 and give report of any that have datastores that don’t start with “sb2”.

Cheers

KC

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host"

foreach($vm in $vms){
   
$esx = Get-View $vm.Runtime.Host -Property Name
   
$ds = Get-View $vm.Datastore -Property Name
   
if($ds | where {$_.Name.Substring(0,3) -notmatch $esx.Name.Substring(0,3)}){
       
$vm | Select Name,
           
@{N="Site";E={if($esx.Name.Substring(0,3) -eq "sa1"){"SiteA1"}else{"SiteB1"}}},
           
@{N="Host";E={$esx.Name.Split('.')[0]}},
           
@{N="DS";E={[string]::Join(',',($ds | %{$_.Name}))}}
    }
}

It should list all VMs that do not follow the rule.


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host"

foreach($vm in $vms){
   
$esx = Get-View $vm.Runtime.Host -Property Name
   
$ds = Get-View $vm.Datastore -Property Name
   
if($ds | where {$_.Name.Substring(0,3) -notmatch $esx.Name.Substring(0,3)}){
       
$vm | Select Name,
           
@{N="Site";E={if($esx.Name.Substring(0,3) -eq "sa1"){"SiteA1"}else{"SiteB1"}}},
           
@{N="Host";E={$esx.Name.Split('.')[0]}},
           
@{N="DS";E={[string]::Join(',',($ds | %{$_.Name}))}}
    }
}

It should list all VMs that do not follow the rule.


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

marklemon
Enthusiast
Enthusiast
Jump to solution

LukD, again, you are the King!  That gave me what I needed and will help make our site resilience one step closer to bulletproof!

Can you give the syntax for excluding certain VMs by name?  We have some SQL Cluster VMs with RDMs which reference the same datastore on both sites that I'd like to leave out of this fine report.

And can you list the full datastores assigned to each VM rather than the first 15-18 characters of the list, please? 

Then it's just a matter of getting this added to vCheck and it's perfect!

If I see you at Euro-VMWorld, I'll be honoured to buy shake your hand, buy you a beer!

(^;

Cheers,

KC

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can exclude specific VMs based on their DisplayName by using the Filter parameter and a RegEx expression.

This

$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host" -Filter @{"Name"="^((?!SQL1|SQL2|EXCH1).)*$"} 

will exclude VMs whose name starts with "SQL1","SQL2" or "EXCH1".

The cut off view of the datastorename is most probably due to the size of the console you are using.

Try piping the Select statement to a Format-Table -Autosize or to a Format-List.


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

marklemon
Enthusiast
Enthusiast
Jump to solution

Finally making some progress with the help of a colleague.  PowerCLI doesn't look completely like a foreign language, but a long way to go before I can write in it like bash. 

Here's some changes that work with filtering out certain VMs and certain hosts.   Figured out how to exclude VMs that are powered off (hurray!).

Still haven't gotten the right code for excluding certain datastores, any suggestions?

$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host","Runtime.PowerState"

foreach($vm in $vms){

    if ($vm.Name -ne "VM9" -and $vm.Name -ne "VM8")

    {

    if ($vm.Runtime.PowerState -eq "poweredOn")

        {

        $esx = Get-View $vm.Runtime.Host -Property Name

        if ($esx.Name.Split('.')[0] -ne "vh15" )

            {

            $ds = Get-View $vm.Datastore -Property Name

            if ($ds | where {$_.Name.Substring(0,2) -notmatch $esx.Name.Substring(0,2)})

           

                {              

                    $vm | Select Name,

                           @{N="Host";E={$esx.Name.Split('.')[0]}},

                           @{N="DS";E={[string]::Join(',',($ds | %{$_.Name}))}} 

                }

            }

        }

    }

   }

0 Kudos
marklemon
Enthusiast
Enthusiast
Jump to solution

Recently I got the code provide by LucD to work in the way I wanted:

http://hypervisorsrus.blogspot.be/2015/08/powercli-vm-host-datastore-affinity.html

Thanks Luc!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're welcome.

For info, Matt and myself created a module for working with rules and groups.

See DRSRule – a DRS rules and groups module


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

0 Kudos