Showing posts with label DRS Rules. Show all posts
Showing posts with label DRS Rules. Show all posts

15 July 2018

Updated DRSRule PowerShell module, now on PSGallery!

A quick blurb on the DRSRule PowerShell module for managing VMware DRS rules via PowerShell/PowerCLI: the module is updated with several bugfixes and enhancements.

Possibly as exciting of news: the module is now available on the official PowerShellGallery, so it is a snap to install/update the module on your machine!

As with any module on the PowerShellGallery, you save the module with the Save-Module cmdlet (so that you can inspect the code and ensure that things are good -- security first!).  And, you install the module with the Install-Module cmdlet.

The straightforward and memorable instructions for saving- and/or installation of a module are available by default on each module's Gallery page. For this module, see https://www.powershellgallery.com/packages/DRSRule.

Enjoy!

22 January 2015

DRSRule - New PowerShell Module for DRS Rule and Group Management

DRS Things
There's a new PowerShell module out, and it is for handling your DRS rule- and group needs!  "Hello, DRS rule/group export and import?  It's me, the new DRSRule module!"  The "why" and the "hows" about the module are further below in this post.

<vNugglets_note> I got to work with the PowerCLI master, Luc Dekens, on this module -- what a treat! (Check out his post about the new module at http://lucd.info/drsrule-drs-rules-and-groups-module)  It was a great learning experience for me, not only on the PowerShell/PowerCLI tip, but also on topics like collaborative/concurrent development, GitHub features (organizations, releases, etc.), and more.

The cmdlets from the module are solid as one would expect:  pipeline support, value by object or name, WhatIf support, and so on.  As for using some of the cool features of PowerShell, the module includes things like advanced/custom type support (including enabling tab-completion of properties later in the pipeline; handy), XML-based external help as manufactured by https://pscmdlethelpeditor.codeplex.com/, and leveraging other object storage formats so as to simplify handling/export/import of rich objects (via JSON) -- just a few of the great features/bonuses by Mr. Dekens. </vNugglets_note>

Now, more about the module.

Why the DRSRule module?

The core PowerCLI PSSnapin from VMware allows for many of the DRS rule operations that one might want to perform.  There have been questions over the years in the VMware Communities PowerCLI forum about how to export DRS rules and groups, and, on how to then import such items.

While there are several posts on the web about how to get/export such things, importing/recreating DRS rules/groups from such exported data has not received much focus.  This was the spark for this DRSRule module:  to allow for easy export/import of such rules/groups.  As part of making a module to handle these actions, other cmdlets were born, like the Get-/New-/Remove-/Set-* cmdlet sets for DRS VM groups and VMHost groups, along with sets for DRS VM to VM rules and VM to VMHost rules.

How to use the DRSRule module?

The DRSRule module comes with an about_DRSRule.
Once the module is installed, use that help topic to get a general introduction to the use of the DRSRule module:
Get-Help about_DRSRule

How to get the DRSRule module?

The source for this PowerShell module is hosted on GitHub.com.
The repository is managed by the GitHub organization "PowerCLIGoodies", and the repo itself is at https://github.com/PowerCLIGoodies/DRSRule.

There are a few ways to get the module, and use it in your PowerShell session:

    Automated

    Via a script
    Use a PowerShell script to perform the download and the extract.
    The script is included in the repository as file DownloadLatest.ps1.

    Via PsGet
    If you have the PsGet module loaded in your PowerShell session, then installing this DRSRule module is as simple as updating PsGet and then installing DRSRule:
    Update-Module -Verbose -Module PsGet
    Install-Module DRSRule

    Manual

    Grab the .zip file of the latest release from the GitHub repository.
    Extract the content to a suitable folder.

    You can go for the Modules folder linked to the Windows account you are using.  To find this path, run:
    Join-Path ${env:\userprofile} "Documents\WindowsPowerShell\Modules"

    Or, you can select any other folder you desire.

    Either way you will end up with a folder named [PathToModule]\DRSRule, in which the PowerShell files for the DRSRule module reside.

    Then, load the DRSRule module into your session:
    If you selected one of the folders in $env:PSModulePath (your PSModule path), you can do this with
    Import-Module DRSRule

    If you picked another folder, you should use the full path to the folder that contains the module's files, like:
    Import-Module [PathToModule]\DRSRule

    Please also read the note below about using Unblock-File, since this module is not Authenticode signed

Note on Unblock-File

This section pertains to using the Manual method above to get the DRSRule module (downloading .zip file).  Since there is no Authenticode certificate supplied for this module, and, assuming that one's PowerShell ExecutionPolicy is at least RemoteSigned, below is a quick/easy way to unblock files.  But, this should only be done when you trust the code in the files that you are unblocking -- inspecting the files is for your own good.
Get-ChildItem [PathToModule]\DRSRule | Unblock-File

27 July 2011

Backup/Export Full DRS Rule Info via PowerShell

Updated 22 Jan 2015
Luc Dekens and I got together on a new PowerShell module for managing/exporting/importing DRS Rules and Groups. See our posts at http://www.lucd.info/2015/01/22/drsrule-drs-rules-and-groups-module and http://www.vnugglets.com/2015/01/drsrule-new-powershell-module-for-drs.html for all of the juicy details. Going forward, that DRSRule module is the way to go. I am leaving this post intact for posterity. Enjoy!
End of update - 22 Jan 2015

We, like many, had the need for a way to backup our DRS rules. Searches turned up many pages, all of which had the same basic recipe: use the Get-DrsRule to grab the rules, get a few properties, and export to CSV.

The problem: Addressing the task getting all of the pertinent info for rules of type ClusterVmHostRuleInfo, or, "VM-to-Host" rules. Rules of this type do not have the VM IDs in a property of the rule, but rather have the VMGroupName and HostGroupNames properties. From these properties, and the .NET view object of the given cluster, one can retrieve the VMs' and hosts' info.

So, we did a little work, and wrote the following to, for all DRS rule types, get the information that would be required if one wanted to recreate the rules:
## Script function: Gather/export DRS rule info, including VMGroup and HostGroup members
## Author: vNugglets.com

## file to which to write the DRS rules' info
$strOutputFilespec = "e:\someFolder\DRSrules_export.csv"

## get cluster .NET view objects, which hold the info about the DRS rules
Get-View -ViewType ClusterComputeResource -Property Name, ConfigurationEx | %{
    ## if the cluster has any DRS rules
    if ($_.ConfigurationEx.Rule -ne $null) {
        $viewCurrClus = $_
        $viewCurrClus.ConfigurationEx.Rule | %{
            $oRuleInfo = New-Object -Type PSObject -Property @{
                ClusterName = $viewCurrClus.Name
                RuleName = $_.Name
                RuleType = $_.GetType().Name
                bRuleEnabled = $_.Enabled
                bMandatory = $_.Mandatory
            } ## end new-object

            ## add members to the output object, to be populated in a bit
            "bKeepTogether,VMNames,VMGroupName,VMGroupMembers,AffineHostGrpName,AffineHostGrpMembers,AntiAffineHostGrpName,AntiAffineHostGrpMembers".Split(",") | %{Add-Member -InputObject $oRuleInfo -MemberType NoteProperty -Name $_ -Value $null}

            ## switch statement based on the object type of the .NET view object
            switch ($_){
                ## if it is a ClusterVmHostRuleInfo rule, get the VM info from the cluster View object
                #   a ClusterVmHostRuleInfo item "identifies virtual machines and host groups that determine virtual machine placement"
                {$_ -is [VMware.Vim.ClusterVmHostRuleInfo]} {
                    $oRuleInfo.VMGroupName = $_.VmGroupName
                    ## get the VM group members' names
                    $oRuleInfo.VMGroupMembers = (Get-View -Property Name -Id ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterVmGroup]) -and ($_.Name -eq $oRuleInfo.VMGroupName)}).Vm | %{$_.Name}) -join ","
                    $oRuleInfo.AffineHostGrpName = $_.AffineHostGroupName
                    ## get affine hosts' names
                    $oRuleInfo.AffineHostGrpMembers = if ($_.AffineHostGroupName -ne $null) {(Get-View -Property Name -Id ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterHostGroup]) -and ($_.Name -eq $oRuleInfo.AffineHostGrpName)}).Host | %{$_.Name}) -join ","}
                    $oRuleInfo.AntiAffineHostGrpName = $_.AntiAffineHostGroupName
                    ## get anti-affine hosts' names
                    $oRuleInfo.AntiAffineHostGrpMembers = if ($_.AntiAffineHostGroupName -ne $null) {(Get-View -Property Name -Id ($viewCurrClus.ConfigurationEx.Group | ?{($_ -is [VMware.Vim.ClusterHostGroup]) -and ($_.Name -eq $oRuleInfo.AntiAffineHostGrpName)}).Host | %{$_.Name}) -join ","}
                    break;
                } ## end block
                ## if ClusterAffinityRuleSpec (or AntiAffinity), get the VM names (using Get-View)
                {($_ -is [VMware.Vim.ClusterAffinityRuleSpec]) -or ($_ -is [VMware.Vim.ClusterAntiAffinityRuleSpec])} {
                    $oRuleInfo.VMNames = if ($_.Vm.Count -gt 0) {(Get-View -Property Name -Id $_.Vm | %{$_.Name}) -join ","}
                } ## end block
                {$_ -is [VMware.Vim.ClusterAffinityRuleSpec]} {
                    $oRuleInfo.bKeepTogether = $true
                } ## end block
                {$_ -is [VMware.Vim.ClusterAntiAffinityRuleSpec]} {
                    $oRuleInfo.bKeepTogether = $false
                } ## end block
                default {"none of the above"}
            } ## end switch

            $oRuleInfo
        } ## end foreach-object
    } ## end if
} | Export-Csv -NoTypeInformation $strOutputFilespec

This bit of code, using the Get-View cmdlet for max speed and min memory footprint, starts off by getting a couple of select properties for all clusters. If the cluster has any DRS rules defined, then the code, for each rule:
  • creates a new PSObject to hold the pertinent info about the rule
  • includes basic rule info, the rule type, and info about the objects involved with the rule, including the VM group names/members and the host group names/members (for both Affine- and AntiAffine host groups)
  • returns the object
  • exports all of the gathered info to CSV for later consumption

Arnim van Lieshout wrote a good post with more information about DRS rules themselves at Managing VMware DRS rules using PowerCLI, and included a few functions for creating VMGroups ("Virtual Machines DRS Groups" in the vSphere client) and HostGroups ("Host DRS Groups"), and for creating new VM-to-Host rules. We will likely be using said functions for our "recreate the DRS rules/groups" work.