Friday, August 10, 2012

DRS Rule Export \ Import

As the title says, this is a Powershell Method for exporting all of your DRS rules to a .txt file that can later be used to import the rules back into a cluster.

It is great for cases where you wish to keep a back of your rules or are looking to migrate to a new vCenter server and do not wish to manually create all your DRS rules again.

Export: 
$outfile = "C:\rules.txt"
$rules = get-cluster | Get-DrsRule
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)   }   $line | Out-File -Append $outfile }

Import: (Assumes your export was written to c:\rules.txt)
$file = "C:\rules.txt"
$rules = Get-Content $file
foreach($rule in $rules){   $ruleArr = $rule.Split(",")   if($ruleArr[2] -eq "True"){$rEnabled = $true} else {$rEnabled = $false}   if($ruleArr[3] -eq "True"){$rTogether = $true} else {$rTogether = $false}   get-cluster $ruleArr[0] | `     New-DrsRule -Name $ruleArr[1] -Enabled $rEnabled -KeepTogether $rTogether\     -VM (Get-VM -Name ($ruleArr[4..($ruleArr.Count - 1)])) }

No comments:

Post a Comment