VMware Cloud Community
rmn77fj40
Contributor
Contributor

Add DRS rule for VM export

Hello,  I have created the following script to export all VMs created in the last 7 days along with number of vcpu's, vram, datastore, notes which works fine.  This report is to be use to validate that the correct template was used, notes added, etc.

However I would also like to include a column for the DRS rule that the VMs belong to, but I am having troubles finding the correct syntax.  Can someone help?  Thank you.

$CDT = Get-Date

Get-VM |`

Get-VIEvent -Types Info -Start $CDT.AddDays(-7) -Finish $CDT |`

Where { $_.fullFormattedMessage -like "deploy*" } | Select @{N="VM"; E={$_.Vm.Name}}, 

@{N="vCPU"; E={(Get-VM -Name $_.Vm.Name).NumCPU}},

@{N="vRAMGB"; E={(Get-VM $_.Vm.Name).MemoryGB}},

@{N="Datastore"; E={(Get-VM -Name $_.Vm.Name) | Get-Datastore}},

@{N="Notes"; E={(Get-VM $_.Vm.Name).Notes}}, FullFormattedMessage, CreatedTime, UserName | Sort CreatedTime | Export-Csv -path .\filename.csv –NoTypeInformation

0 Kudos
2 Replies
LucD
Leadership
Leadership

If you use our DRSRule module, you can do something like this.

I only show how to link the VM name with the DRS rule(s) to which it belongs. It should be trivial to incorporate in your script

$ruleTab = @{}

foreach($rule in Get-DrsVMToVMRule){

    $rule.VM | %{

        if(!$ruleTab.ContainsKey($_)){

            $ruleTab.Add($_,@($rule.Name))

        }

        else{

            $ruleTab[$_] = $ruleTab[$_] + $rule.Name

        }

    }

}

foreach($rule in Get-DrsVMToVMHostRule){

    Get-DrsVMGroup -Name $rule.VMGroupName | %{

        $_.VM | %{

            if(!$ruleTab.ContainsKey($_)){

                $ruleTab.Add($_,@($rule.Name))

            }

            else{

                $ruleTab[$_] = $ruleTab[$_] + $rule.Name

            }

        }

    }

}

Get-VM |

Select Name,@{N='DRS Rule';E={$ruleTab[$_.Name] -join '|'}}


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

0 Kudos
rmn77fj40
Contributor
Contributor

Thank you.  I've taken a look at that post.  Because of policy, I'm not sure I can get the module into our environment.

I know I can use the below to gather the DRS Rule name for the VMs.  But I am having a hard time getting it to work with the Get-VMEvent cmdlet.

Get-VM |Select Name, Notes, @{N="ESX Cluster";E={Get-Cluster -VM $_}}, @{N="DRSRule"; E={Get-Cluster | Get-DRSRule -VM $_}}


Do you have any suggestions on incorporating this into the original?


(We are only using VM-VM Anti-affinity rules in out environment)

0 Kudos