VMware Cloud Community
shaggy_041086
Enthusiast
Enthusiast
Jump to solution

DRS-Backup Script

Hi all,

I have a script to backup all DRS-Rules in current vCenter and to import it into new vCenter for migration, but have few VMs to Host rules "Run VMs on Hosts" type. So I found that there is a way around to create such a Rule by creating VM groups and Host groups and then the rule. But I can't find how to get the already existing groups in old vCenter and import it in the new vCenter. This is what I am using for Import and export of VMAffinityType rules -

Export -

if(Test-Path -Path $outfile){
     Remove-Item $outfile
}
get-datacenter | %{
     $dcName = $_.Name
     $_ | Get-Cluster | %{
          $clusterName = $_.Name
          $rules = $_ | Get-DrsRule
          if($rules){
               foreach($rule in $rules){
                    $line = $dcName + "," + $clusterName
                    $line += ("," + $rule.Name + "," + $rule.Enabled + "," + $rule.KeepTogether)
                    foreach($vmId in $rule.VMIds){
                              $line += ("," + (Get-View -Id $vmId).Name)
                    }
               $line | Out-File -Append $outfile
               }
          }
     }
}

Import -

$rules = Get-Content $outfile

foreach($rule in $rules){

     $ruleArr = $rule.Split(",")

     if($ruleArr[3] -eq "True"){$rEnabled = $true} else {$rEnabled = $false}

     if($ruleArr[4] -eq "True"){$rTogether = $true} else {$rTogether = $false}

     Get-Datacenter $ruleArr[0] | Get-Cluster $ruleArr[1] | `

     New-DrsRule -Name $ruleArr[2] -Enabled $rEnabled -KeepTogether $rTogether -VM (Get-VM -Name ($ruleArr[5..($ruleArr.Count - 1)]))

}

These are LUCD's scripts !!

Can anyone suggest a good way to integrate what I am looking for into these??

Attaching some screens showing my current Rules

Thanks,

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you already see Need a script to export&import DRS rules. ?

In that thread the rules (including the groups) are exported with the script from vNugglets, and I provided a modified version of Arnim's functions to read the rules back. At the end of the script attached to that post is my script to read the info back from the CSV file.

Does that do what you want ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at Arnim's Managing VMware DRS rules using PowerCLI post.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

shaggy_041086
Enthusiast
Enthusiast
Jump to solution

I did go through those blog posts, but they just creates a HostGroup or VMGroup and creates a rule which maps Hosts to VMs.

What if I have those already and want to export them to a CSV or TXT file like your script did for VMAntiAffinity rules.

It would be a great idea to back them up too.

Please if you can let me know how I can go, or anything would be helpful!

Thanks LucD!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you already see Need a script to export&import DRS rules. ?

In that thread the rules (including the groups) are exported with the script from vNugglets, and I provided a modified version of Arnim's functions to read the rules back. At the end of the script attached to that post is my script to read the info back from the CSV file.

Does that do what you want ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos