PowerCLI: Export, Import and Create DRS Rules v2


image

In my first post about this subject, I showed a script create by the PowerCLI Master @LucD. A couple of minutes later Hugo Peeters posted a comment why I did not used the object-oriented approach of Powershell. He also posted an example. So in this post I will show you the other way by using Export/Import-CliXml.

First we start with the export part. The following one-liner will export all the DRS Rules to a XML file:

# Export DRS
Get-Cluster -Name "Cluster_01" | Get-DrsRule | `
Export-CliXml 'C:\scripts\drs.xml'

The XML file look like this:

<?xml version="1.0" encoding="utf-16"?>
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>VMware.VimAutomation.Client20.DrsVMAffinityRuleImpl</T>
      <T>System.Object</T>
    </TN>
    <ToString>VMware.VimAutomation.Client20.DrsVMAffinityRuleImpl</ToString>
    <Props>
      <S N="Name">production</S>
      <B N="Enabled">true</B>
      <S N="ClusterId">ClusterComputeResource-domain-c7</S>
      <B N="KeepTogether">true</B>
      <Obj N="VMIds" RefId="1">
        <TN RefId="1">
          <T>System.String[]</T>
          <T>System.Array</T>
          <T>System.Object</T>
        </TN>
        <LST>
          <S>VirtualMachine-vm-23</S>
          <S>VirtualMachine-vm-27</S>
          <S>VirtualMachine-vm-29</S>
          <S>VirtualMachine-vm-33</S>
        </LST>
      </Obj>
    </Props>
  </Obj>
</Objs>

The next one-liner will import the XML file, which we earlier created with the export one-liner. After the import, the one-liner will create a DRS rule:

# Import DRS
ForEach ($rule in (Import-CliXml 'C:\scripts\drs.xml')){
    New-DrsRule -Cluster (Get-Cluster -Name "Cluster_01") `
    -Name $rule.Name -Enabled $rule.Enabled `
    -KeepTogether $rule.KeepTogether `
    -VM (Get-VM -Id $rule.VmIds)}

 

If you want to see the DRS Rules for a cluster via the PowerCLI, You can run the following one-liner:

# DRS Rules One-Liner
Get-DrsRule -Cluster (Get-Cluster -Name Cluster_01) `
| Sort Name | Select Name, @{N="Enabled";E={$_.Enabled}}, `
@{N="KeepTogether";E={$_.KeepTogether}},`
@{N="Virtual Machines";E={$_.VMIds | % { Get-VM -Id $_ | `
% {$_.Name} }}} 

The following output will be generated:

Name Enabled KeepTogether Virtual Machines
appliances False False Nagios, vMA
production True True DC01,VC01,MC01,MAIL01

Before you can create a XML file with your new rules, you need to get the ID’s of the Cluster and the VM. You can get these ID’s via the following one-liner:

Get-VM | Sort Name | Select Name, `
@{N="VM ID";E={$_.Id}}, `
@{N="Cluster Name";E={Get-Cluster -VM $_}}, `
@{N="Cluster ID";E={Get-Cluster -VM $_ | % {$_.Id}}} | fl *

The output will look like this.

image

or you can export it to a CSV file:

Get-VM | Sort Name | Select Name, `
@{N="VM ID";E={$_.Id}}, `
@{N="Cluster Name";E={Get-Cluster -VM $_}}, `
@{N="Cluster ID";E={Get-Cluster -VM $_ | % {$_.Id}}} | `
Export-CSV -NoTypeInformation "D:\scripts\ps\vmids.csv"

 

Now you have to create a XML file. You can use a file, you created with the export DRS one-liner and change the following lines:

image

When you’re done with the XML file, you can run the Import DRS one-liner to import your new Rules. When you open the Cluster properties you will see that the new rule is added:

image

Advertisements

3 thoughts on “PowerCLI: Export, Import and Create DRS Rules v2

  1. Pingback: Twitted by afokkema

  2. Yaz

    Even though XML is human readable it shouldn’t be human editable (like everyother data file in my opinion). Why not present a way to change certain values before exporting it to a XML file. Or, if its a common baseline, change settings after importing but before committing it to the new platform? That way mistakes are not made so easily and it can’t be forgotten.

    If your staying within the same VI then you could skip the import/export all along.

    Btw, i like most of your scripts. But try not to import and export as frequent as you do. I’ve seen scripts where you do that within the same script just a few lines apart. Keep it within objects and if you need extra data (records/fields) you can append that with the add-member CMDlet.

    Reply
  3. Pingback: Backup and Restore DRS Rules with the VESI « ICT-Freak.nl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s