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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#####################################################
#
# 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

 

Share This:

10 thoughts on “Script to backup your vCenter DRS Rules

    1. Sure, you can use something like this to restore to a new switch.

      Get-ChildItem “E:\vmware\DRS\vCenter” | Foreach {
      New-VDPortgroup -VDSwitch NewVDS -Name “New$($_.BaseName)” -BackupPath $_.FullName
      }

      Be sure to validate everything once you restore.

  1. Hello,

    Looking to do the same however on 6.7 has anything changed? I keep getting errors that say it is not connected to a vCenter server, even thought it showing that it is.

    “get-cluster : 1/17/2020 3:36:56 PM Get-Cluster You are not currently connected to any servers. Please connect first using a Connect cmdlet. ”

    Even manually going Connect-ViServer before hand and connecting, the error still appears.

    1. I just tested the script in my homelab and had no problems. If you do Connect-viserver and then get-cluster, it should return a list of clusters. In my lab, I only have 1 cluster.

      1. Hi there,

        I found a good script to save the DRS rules in a VMware article. Now I would like to import the rules from the files again. Could you maybe help me?

        Many Thanks

        Here the backup-script:

        # Export DRS Rules to CSV

        # Requires PowerCLI 6.5.1+

        # Export DRS Rules

        Get-Cluster | Get-DRSRule | select Name,Cluster,Enabled,Type,@{N=’VMs’;E={[string]::Join(‘,’,((Get-VM -ID $_.VMIDs).Name))}} | Export-CSV -NoType -Delimiter ‘;’ -Encoding Default D:\PS-Scripts\Ausgabe\DRSRule.csv

        # Export DRSClusterGroup

        Get-Cluster | Get-DRSClusterGroup | Select Name,Cluster,Grouptype,@{N=”Member”;E={[string]::Join(‘,’,($_.Member))}} | Export-CSV -NoType -Delimiter ‘;’ -Encoding Default D:\PS-Scripts\Ausgabe\DRSClusterGroup.csv

        # Export DRSVMHostRule

        Get-Cluster | Get-DRSVMHostRule | Select Name,Cluster,VMGroup,Type,VMHostGroup,Enabled | Export-CSV -NoType -Delimiter ‘;’ -Encoding Default D:\PS-Scripts\Ausgabe\DRSVMHostRule.csv

          1. Hi Alex,

            As I posted back in 2017, you can use this command to restore from a backup file. This will restore each backup file into a new VDS for each. If there’s only 1 backup in the folder, it’ll only restore that one.

            Get-ChildItem “E:\vmware\DRS\vCenter” | Foreach {
            New-VDPortgroup -VDSwitch NewVDS -Name “New$($_.BaseName)” -BackupPath $_.FullName
            }

  2. Have you tried the new way with the Get-DrsVMHostRule command let. the get-drsrule one is deprecated now!.

    Thank you.

Leave a Reply to Ben Liebowitz Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.