PowerCLI: Retrieving DRS rules

I was recently looking to retrieve a list of DRS rules in each cluster managed by a single vCenter server and to export the output using PowerCLI.

The script will be required to be invoked as a scheduled task and therefore we will specify two paramaters. Firstly the VI Server we will be required to establish a connection to, where by default this will use the host name and require a mandtory parameter for the output directory for exporting the output.

Param ([string] $vCenter = ([System.Net.Dns]::GetHostByName(($env:computerName))).HostName, [parameter(Mandatory=$true)][string] $Output) 

Now, we will load the vSphere PowerCLI snap-in to the current powershell session and establish a connection to the VI Server.

if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) 
    Add-PSSnapin VMware.VimAutomation.Core | Out-Null 

Connect-VIServer $vCenter | Out-Null

Once a connection to the VI Server has been established we will retrieve all the DRS rules from each cluster and store this as a collection.

$DRSRules = Get-Cluster | Get-DrsRule

For each DRS rule returned in the collection we will retrieve the following property values and store the results in a variable to later export to a CSV file.

  • Cluster Name
  • DRS Name
  • DRS Enabled
  • DRS Type
  • VM Names

In order to retrieve the VM names, we will require to retrieve the name from the retrieved VM Id, by splitting the property value and for each VM invoking the Get-VM cmdlet to retrieve the name value and then join each returned VM name to a single string.

$Results = ForEach ($DRSRule in $DRSRules)
    "" | Select-Object-Property @{N="Cluster";E={(Get-View- Id $DRSRule.Cluster.Id).Name}},
    @{N="DRS Type";E={$DRSRule.KeepTogether}}, 
    @{N="VMs";E={$VMIds=$DRSRule.VMIds -split "," 
      $VMs = ForEach ($VMId in $VMIds) 
        (Get-View -Id $VMId).Name
      $VMs -join ","}}

Finally, we will export the output to a CSV file and close the connection to the VI Server.

$Results | Export-Csv -NoTypeInformation -Path ($Output + "\" + $vCenter+ "_DRS Rules.csv") -Force
Disconnect-VIServer -Server $vCenter -Confirm:$False

5 thoughts on “PowerCLI: Retrieving DRS rules

  1. Hello Dean, I’m looking to write a script that collects DRS settings (enabled, fully automated, etc.) and emails when they are changed. Your commands look perfect for me. But I’m getting an error that I hope you can help me with easily:

    The term ‘Select-Object-Property’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:3 char:32
    + “” | Select-Object-Property <<< Get-PSSnapin VMware.VimAutomation.Core

    Name : VMware.VimAutomation.Core
    PSVersion : 2.0
    Description : This Windows PowerShell snap-in contains Windows PowerShell cmdle
    ts for managing vSphere.

    Can you help?



  2. Just wanted to say thank you. Needed to get a list of DRS rules and was disappointed Get-DrsRule returns VM ID instead of VM name. Not much of a scripter myself, I went searching for help and found your page. Had to tweak the code a bit but got it working and does exactly what I needed. Thank you! Appreciate your taking the time to post this.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s