VMware Cloud Community
jasonrobinson
Enthusiast
Enthusiast

Export DRS Rules to CSV

All,

I am trying to export all of our DRS Rules to a CSV file. I am very close to having exactly what I need however in the CSV it is listing each Rule twice. Any idea on how I can correct that. Here is a copy of the script I am running. Also if there is anyway to improve on it please let me know. Thanks

$report = @()

$rules = Get-Cluster | Get-DrsRule

foreach ($rule in $rules) {

$row = "" | Select ClusterName, RuleName, RuleEnabled, KeepTogether, VMs

$row.ClusterName = (Get-View -Id $rule.ClusterId).Name

$row.RuleName = $rule.Name

$row.RuleEnabled = $rule.Enabled

$row.KeepTogether = $rule.KeepTogether

$vms = ""

foreach ($vmId in $rule.VMIds) {

$vms += ((Get-View -Id $vmId).Name + ",")

$row.VMs = $VMs

$report += $row

}

}

$report | Export-Csv .\DRSrules.csv -NoTypeInformation

Jason @jrob24
Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

Did you already have a look at ?

There is a script that does something similar. And it will allow you to import the rules!

The reason why you saw each rule twice is because you stored the rule in the $report array for each the 2 guests in the affinity rule (inside the $rule.VMIds loop).

This version will store the rule only once (and with both guest names)

$report = @()
$rules = Get-Cluster | Get-DrsRule
foreach ($rule in $rules) {
	$row = "" | Select ClusterName, RuleName, RuleEnabled, KeepTogether, VMs
	$row.ClusterName = (Get-View -Id $rule.ClusterId).Name
	$row.RuleName = $rule.Name
	$row.RuleEnabled = $rule.Enabled
	$row.KeepTogether = $rule.KeepTogether
	$vms = ""
	foreach ($vmId in $rule.VMIds) {
		$vms += ((Get-View -Id $vmId).Name + ",")
	}
	$row.VMs = $VMs
	$report += $row
}
$report | Export-Csv .\DRSrules.csv -NoTypeInformation


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

Reply
0 Kudos
jasonrobinson
Enthusiast
Enthusiast

Doh!!! Something so small and of course I overlooked it. Thanks for your help Luc.

Jason @jrob24
Reply
0 Kudos
Jameel21
Contributor
Contributor

After running the script the csv is empty, any suggestion?

Reply
0 Kudos
LucD
Leadership
Leadership

Did you check that

Get-Cluster | Get-DrsRule

return anything?


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

Reply
0 Kudos
Jameel21
Contributor
Contributor

Get-Cluster | Get-DrsRule

It does not return anything, I have connected to vCenter, but still shows nothing

Reply
0 Kudos
Jameel21
Contributor
Contributor

I got it, what I overlooked, Is there any script to extract DRS Rules under "VM/Host rules"  and VM Overrides"?

Reply
0 Kudos
LucD
Leadership
Leadership

By default, the Get-DrsRule cmdlet only returns affinity and anti-affinity rules.
As the cmdlet explains you will need to use the Type or VMHost parameter if you want to see VMHostAffinity rules.


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

Reply
0 Kudos