PowerCLI – Migrating DRS rules between vCenter Servers

I haven’t written a blog post in forever, been busy with a big vSphere 6.0 distributed deployment.

Part of the vSphere 6 deployment i had to migrate DRS rules from a windows based vCenter deployment to a VCSA based deployment and copying all your DRS rules across can be a tedious process. I had a plethora of DRS rules to migrate across multiple clusters and was not looking forward to creating all the rules manually. Luckily  Luc Dekens and Matt Boren put together a DRSRule module which made the task easy. More info about the module can be found here.

I have put together a PowerShell script that will migrate all the rules for you.

Note : Updated to Import-Module and not PSSnapin for PowerCli 6.5.x

 

Param (
        [String]$my_old_vc,
        [string]$the_cluster,
        [string]$my_new_vc
)
if (!$my_old_vc -or (!$the_cluster) -or (!$my_new_vc))
{
        Throw "You need to supply all parameters."
}

# Import VIM automation core snapin if not loaded

$imported = Get-Module -Name VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'

if (!$imported)
{
        Write-Host -ForegroundColor "yellow" "Importing VMware.VimAutomation.Core..."
        try
        {
                Import-Module VMware.VimAutomation.Core -ErrorAction 'Stop'
        }
        catch
        {
                Write-Host "Something went wrong. ""VMware.VimAutomation.Core"" snapin not found. Try execute the script from PowerCli"
        }
}
# Import DRS Module

try
{
        Import-Module DRSRule -ErrorAction 'Stop'
}
catch
{
        write-host "DRSRule Module not found. Did you install it correctly ?. Check http://www.lucd.info/2015/01/22/drsrule-drs-rules-and-groups-module/"
}

cls

Write-Host "Welcome to the DRSRule migration tool."
Write-Host "You will need to have full administrative access to both vCenter management serevers to proceed."
Write-Host
Write-Host "============================================="
Write-Host
Write-Host "The info you supplied:"
write-host
Write-Host " Old_VC  : $my_old_vc"                      
Write-Host " New_VC  : $my_new_vc"
Write-Host " Cluster : $the_cluster"
write-host
Write-Host "============================================="

Read-Host "Hit Enter to continue."

cls
try
{
        Connect-VIServer $my_old_vc -WarningAction 'SilentlyContinue' -ErrorAction 'Stop'
}
catch
{
        write-host "Unable to connect to the old vCenter Management Server $my_old_vc"
        break
}

write-host "Connected to $my_old_vc."

write-host "Collecting DRS Information. Please Wait."

$drshostgrps = get-cluster -name $the_cluster| get-DRSVMHostGroup 
$drsvmgrps = get-cluster -name $the_cluster |Get-DRSVmGroup
$drs_vm2hosts = get-cluster -name $the_cluster |Get-DrsVMToVMHostRule
$drs_vm2vms = get-cluster -name $the_cluster |Get-DrsVMToVMRule

write-host "Done."

write-host "Disconnect from $my_old_vc"

Disconnect-VIServer $my_old_vc -Confirm:$false

try
{
        Connect-VIServer $my_new_vc -WarningAction 'SilentlyContinue' -ErrorAction 'Stop'
}
catch
{
        write-host "Unable to connect to the new vCenter Management Server $my_new_vc"
        break
}

write-host "Connected to $my_new_vc."
write-host "Start applying DRS Information. Please Wait."

foreach ($drshostgrp in $drshostgrps)
{
        New-DRSVMHostGroup -name:$drshostgrp.name -cluster:$drshostgrp.cluster -vmhost:$drshostgrp.vmhost

write-host "DRSRule VMHost Groups Created."
}

foreach ($drsvmgrp in $drsvmgrps)
{
        New-DrsVMGroup -name:$drsvmgrp.name -cluster:$drsvmgrp.cluster -vm:$drsvmgrp.vm

write-host "DRSRule VM Groups created."
}
foreach ($drs_vm2host in $drs_vm2hosts)
{
        new-DRSVMToVMHostRule -name:$drs_vm2host.name -cluster:$drs_vm2host.cluster -enabled:$drs_vm2host.enabled -mandatory:$drs_vm2host.mandatory -VMGroupName:$drs_vm2host.vmgroupname -AffineHostGroupName:$drs_vm2host.AffineHostGroupName -AntiAffineHostGroupName:$drs_vm2host.AntiAffineHostGroupName

write-host "DRSRule VM to VMHost Rules created"
}

foreach ($drs_vm2vm in $drs_vm2vms)
{
        New-DrsVMToVMRule -name:$drs_vm2vm.name -cluster:$drs_vm2vm.cluster -enabled:$drs_vm2vm.enabled -KeepTogether:$drs_vm2vm.keeptogether -vm:$drs_vm2vm.vm
        
        write-host "DRSRule VM to VM Rules created."
}

write-host "All done. Please validate all rules manually on the clusters and vcenter management servers."

write-host "Disconnecting from the VC's"

Disconnect-VIServer * -confirm:$false

write-host "Disconnected"

Leave a Reply

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