Thursday, June 20, 2013

Using PowerCLI to export DRS details...vCenter 5.1 migration part 2

Ok, this past week has been busy as I still have tons of hosts to get up to ESXi 5.1...I'm just so glad vCenter is done :)  Now there are quite a few details that can be contained within DRS and I knew what settings I used and wrote the scripts specifically for those.

For DRS, I just needed to know the rules (affinity and anti-affinity)

Lets look at how I dumped the DRS rules (I'm obviously not the only one who has done this):


param(
 [Parameter(Mandatory=$true)][string] $VIserver = $(Read-Host -prompt "vCenter?")
)

$vc=connect-viserver $VIserver

$outfile = "rules_$($vc.name).csv"
$rules = get-cluster -server $vc| Get-DrsRule
$output=@()
if ($rules -ne $NULL)
{
 foreach($rule in $rules){
   $line=""|select Cluster,Name,Enabled,KeepTogether,VM1,VM2
   $line.cluster = (Get-View -Id $rule.ClusterId).Name
   $line.name = $rule.Name 
   $line.Enabled = $rule.Enabled
   $line.KeepTogether = $rule.KeepTogether
   $line.VM1 = (get-view -id ($rule.VMIds)[0]).name
   $line.VM2 = (get-view -id ($rule.VMIds)[1]).name
   $output+=$line
 }
}
$output|export-csv -notypeinformation $outfile
disconnect-viserver * -confirm:$false -force

...now, of course an import script would make this so much more useful.

param(
 [Parameter(Mandatory=$true)] [string] $inputfile = $(Read-Host -prompt "input file?"),
 [Parameter(Mandatory=$true)] $VIserver = $(Read-Host -prompt "VIserver?")
 )

$vc=connect-viserver $VIserver
$rules=import-csv -path $inputfile
if($rules -eq $NULL){throw "Could not read $inputfile"}

$clusters=$rules|select cluster|sort cluster|Get-Unique -AsString|%{$_.cluster}
foreach ($cluster in $clusters)
{
 $clusterrules = $rules|?{$_.cluster -eq $cluster}
 $clusterobj=get-cluster $cluster
 foreach ($rule in $clusterrules)
 {new-drsrule -cluster $clusterobj -Name $rule.name -Enabled ([system.convert]::toBoolean($rule.enabled)) `
  -KeepTogether ([system.convert]::toBoolean($rule.keeptogether)) -VM (get-vm $rule.vm1,$rule.vm2)} 
}

disconnect-viserver * -confirm:$false -force

Alright, there we have it. First script dumps ALL rules for all clusters in a vCenter, while the second script creates those rules on the corresponding clusters in the new vCenter. I know I haven't gone over the cluster creation scripts yet, but I thought it was better to go over the export/import in one post, not exactly chronological, very much related.

No comments:

Post a Comment