PowerCLIGoodies / DRSRule

PowerShell module with support for managing vSphere DRS Rules and Groups
29 stars 9 forks source link

Check if VM is in a DrsVMGroup #7

Closed m4c3 closed 7 years ago

m4c3 commented 7 years ago

Hi, is there a way to check if a VM is already in a DrsVMGroup? Or can i load all the VM Names from the Group and match them by myself?

PowerCLI C:\> Get-DrsVMGroup -Name "All VMs" -Cluster DMZ -ReturnRawGroup

Vm          : {VirtualMachine-vm-720, VirtualMachine-vm-706, VirtualMachine-vm-703, VirtualMachine-vm-66968...}
LinkedView  :
Name        : All VMs
UserCreated : True
UniqueID    :

PowerCLI C:\> Get-DrsVMGroup -Name "All VMs" -Cluster DMZ

Name                     Cluster          UserCreated   VM
----                     -------          -----------   --
All VMs                 DMZ              True          {FOO-P01, BAR-P01, JADA-P01, LANA-T01...

Thanks Matthias

mtboren commented 7 years ago

Greets, @m4c3-

Right, get a DrsVMGroup by the related VM -- sounds like a great use case. I'd think that we would add something like -VM or -RelatedObject to Get-DrsVMGroup so as to enable such things. Then we could do:

C:\> Get-VM myVM0 | Get-DrsVMGroup
Name               Cluster        UserCreated   VM
----               -------        -----------   --
testVMGrp0         myClu0         True          {myVM0, dtest4}

How would that be?

cerosunos commented 7 years ago

Hello really interesting question. I would like to retrieve all the VMs in a cluster not belonging to a DRS group. It's very common new VMs where created but an administrator could mistake the inclusion on a DRS group.

Any idea to create this code?

mtboren commented 7 years ago

Hey, @m4c3

I made a commit to the feat_RelatedObj branch (commit 0dade0a) that adds the ability to get the DrsVMGroup by the subject VM. Should work like:

C:\> Get-VM myVM0 | Get-DrsVMGroup
Name               Cluster        UserCreated   VM
----               -------        -----------   --
testVMGrp0         myClu0         True          {myVM0, dtest4}

How does that do for you? And, I intend to add similar behavior to the other Get-Drs* cmdlets, starting with the Get-DrsVMHostGroup cmdlet.

cerosunos commented 7 years ago

@mtboren I was working on a very quick script with the customer just to retrieve something similar what I was asking for. It's obvious that could be many different ways to perform this task, and if could be added to the code, some kind of function or maybe on your last post something like: Returning false if any given VM is not included on any DRSRule. Could be possible?

Code generated for customer:

$vmrule = Get-DrsVMGroup -Cluster “" | select vm |%{$_.VM} > d:\milog.csv $vmrule = type d:\milog.csv

foreach($vm in Get-VM){ $VMname = $vm.Name.ToString()

 if ($vmrule -like "*$VMname*")
 {#Write-Host -ForegroundColor Green "Estoy"
 }
 Else{Write-Host -ForegroundColor red "La VM $VMname no está en ninguna DRS rule "}

}
mtboren commented 7 years ago

Hello, @cerosunos

Yes, interesting question indeed. We should be able to meet your need with the v1.0.3 version of the module. In order to get all of the VMs in cluster that are not in a DrsVMGroup, we could do something like:

$arrDrsVMGroups = Get-Cluster myCluster | Get-DrsVMGroup -ReturnRawGroup
## get the VMs in "myCluster" that are not in any DrsVMGroup
Get-Cluster myCluster | Get-VM | Where-Object {$arrDrsVMGroups.Vm -notcontains $_.Id}

This has a couple of advantages over some potential other ways:

How does that suit your need? Cheers

cerosunos commented 7 years ago

@mtboren your script really works, thanks.

mtboren commented 7 years ago

Alright -- released v1.1.0 in (commit https://github.com/PowerCLIGoodies/DRSRule/commit/c8c8e2899bb9dcb4da7a8a6d95070aa0cbd457e0), in which this feature (and some other related, expanded capabilities) are included. Thanks for the suggestion, @m4c3 !