Skip navigation
VMware

This Question is Answered (go to answer)

2,004 Views 25 Replies Last post: Jun 2, 2010 11:28 AM by LucD RSS
1 2 Previous Next
DennieTidwell Enthusiast 99 posts since
Mar 27, 2007
Currently Being Moderated

Feb 21, 2009 7:25 PM

Backup/Restore DRS VM affinity/anti-affinity rules - can these be backed up and restored without manually re-entering after DRS disable?

 

DRS VM affinity/anti-affinity rules - these are lost when DRS is disabled - they can be "listed" via perl script or VI Toolkit for manual re-entering if needed ...

 

 

But can they be backed up / restored via automated methods for easy restoration in case of required or accidental disabling of DRS? What if a vCenter user disables instead of setting to manual during operations?

 

 

I have over 100+ DRS VM affinity/anti-affinity rules to maintain.

 

 

JoeLyons Hot Shot 121 posts since
Feb 19, 2009

 

I havnt tried this but there is a set-drsrule in the vi toolkit,

 

 

It might work for you if you use get-drsrule and output it to a  text file first.

 

 

Joe

 

 

Remember to back EVERYTHING up before you change ANYTHING and consider awarding points if answers where helpful to you.
LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005

There are indeed several DRS rules related cmdlets in the VITK 1.5.

 

The following script will save all rules to a .txt file.

$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 
}

The reason we write the rules to a .txt file is because the "Keep together" type of rules can have 2 or more guests defined.

This type of info (variable length array) is impossible to export to a .CSV file.

 

The 2nd script reads this external file and defines the rules.

$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)])) 
}

The trick used for the guests is that we select part of an array with the \[a..b\] notation.

 

Currently you will have to run the first script for each cluster separately but this could easily be adapted to run against all cluster in your VI.

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
milling Novice 4 posts since
Aug 9, 2006

 

Hi,

 

 

thanks for the scripts LucD. They did help but I had some problems with the restore script. When I ran the restore it complained about "Missing or invalid array index expression". I changed the last line from "(Get-VM -Name ($ruleArr[http://4..($ruleArr.Count - 1)|http://4..($ruleArr.Count - 1)]))" to "(Get-VM -Name ($ruleArr[http://4..($ruleArr.Count - 1)|http://4..($ruleArr.Count - 1)]))" and the restore ran successfully.

 

 

Thanks,

 

 

M

 

 

 

 

 

milling Novice 4 posts since
Aug 9, 2006

Hi,

 

that might be the problem. Something is adding the "http://" to the script when its posted. I resolved the issue this way "(Get-VM -Name ($ruleArr[http://4..($ruleArr.Count - 1)|http://4..($ruleArr.Count - 1)]))" without the "http://"

 

Thanks

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005

Yes, the forum SW has problems with square brackets.

 

I attached both scripts.

Attachments:
Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
milling Novice 4 posts since
Aug 9, 2006
jlewko98 Novice 8 posts since
May 20, 2010

 

When trying to run this script I am getting the following error:

 

 

 


<span style="color: #ff0000">Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\DRS-export-rules.PS1:7 char:24
+   $line = (Get-View -Id <<<<  $rule.ClusterId).Name
+ CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\DRS-export-rules.PS1:10 char:34
+     $line += ("," + (Get-View -Id <<<<  $vmId).Name)
+ CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView</span>

 

 

I have changed the script so that

$clusterName =&lt;cluster-name&gt;

is now

$clusterName = "Temp"     (this is a test cluster that I am using for this script).

 

 

 

I apologize as I am not very savvy with scripting, let alone the vSphere PowerCLI, and am trying to learn. Any help / pointing in the right direction is much appreciated.

 

 

 

 

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005

It could be that you have no DRS rules defined on the cluster.

And PowerShell will always execute the loop once, even if the collection is $null.

 

With this updated version, the problem is avoided.


$outfile = "C:\rules.txt"
Remove-Item $outfile
$clusterName = <cluster-name>
$rules = get-cluster -Name $clusterName | Get-DrsRule

if($rules){
     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 
     }
}

 

 

 

____________

Blog: LucD notes

Twitter: lucd22

Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
jlewko98 Novice 8 posts since
May 20, 2010

 

LucD,

 

 

  Thank you, the script is working now. I appreciate your expertise and time.

 

 

Bookmarked By (0)

Share This Page

Communities