Export/Import DRS affinity roles

Two simple script for export and import DRS roles from vcenter

 

Download  ExportDRS

$outfile = “C:\rules.txt”
Remove-Item $outfile
$clusterName = <cluster-name>
$rules = get-cluster -Name $clusterName | 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
}

 

Download  ImportDRS

$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[http://4..($ruleArr.Count – 1)|http://4..($ruleArr.Count – 1)]))

}


84 total views, 1 views today