The Lowercase w

Post info:

Script to backup your vCenter DRS Rules

I was thinking yesterday, I have a script that I run on a monthly basis to backup my vDS configs…  Shouldn’t I also have something that does the same for my DRS rules?  So, I wrote a script that I can schedule to run on a weekly/monthly basis to backup my DRS rules. I have multiple vCenter servers, so I used an array to connect to each one and then exported the rules for each cluster within those vCenters.


#####################################################
#
# PowerCLI Script to export DRS Rules
# Created by BLiebowitz on 6/26/2017
#
#####################################################

# Build an array for each vCenter to connect to
$array = "vCenter1", "vCenter2", "vCenter3"
for($count=0;$count -lt $array.length; $count++)
{
# Connect to vCenter
connect-viserver $array[$count]

# Set the export file name & folder location.
$export = "E:\vmware\DRS\$($array[$count])_DRSrules_$((Get-Date).ToString('MM-dd-yyyy')).txt"

# Get the clusters to export rules from
$clusters = get-cluster

# Find and export rules for each cluster
foreach ($cluster in $clusters) {

# Find all the rules
$rules = get-cluster -Name $cluster | Get-DrsRule

# Export each rule
foreach($rule in $rules){
$line = (Get-View -Id $rule.ClusterId).Name
$line += ("," + $rule.Name + "," + $rule.Enabled + "," + $rule.KeepTogether)

foreach($vmId in $rule.VMIds){
$line += ("," + (Get-View -Id $vmId).Name)
}
# Write each rule to a file.
$line | add-content $export –force

# Disconnect from vCenter
disconnect-viserver $array[$count] –confirm:$false

}
}
}

The results will look like this:

An Affinity Rule will look like:
Cluster1,Rule_Name,True,True,VM1,VM2

An Anti-Affinity Rule will look like:
Cluster1,Rule_Name,True,False,VM1,VM2

This is what the line means… CLUSTERNAME, RULENAME, Enabled/Disabled, Affinity=True/Anti-Affinity=False, VMs

Hope someone finds this useful. 🙂

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

 

Leave a Reply

Your email address will not be published. Required fields are marked *