6 Replies Latest reply on Apr 20, 2017 6:18 AM by thomasfoinLVDN

    Add VM to DRS Rule

    mcescalante Novice

      I am working on a script and a PowerCLI commandlet seems to already be written for it. The script adds an existing vm to an existing DRS Rule.


      Here is the code for the commandlet I would like to replicate:


      $spec = New-Object VMware.Vim.ClusterConfigSpecEx
      $spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[] (1)
      $spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
      $spec.groupSpec[0].operation = "edit"
      $spec.groupSpec[0].info = $DrsGroup
      $spec.groupSpec[0].info.vm += $VM.ExtensionData.MoRef

      $Cluster.ExtensionData.ReconfigureComputeResource_Task($spec, $true)

      To my understanding, something along the following lines can be done to translate this to Perl:

      1. Create a ClusterGroupSpec object with the correct information. I fail here, and am unsure of how to add "info.vm" into ClusterGroupSpec in Perl.

           my $cluster_group_spec = ClusterGroupSpec->new(operation => "edit", info => $drs_rule_name...

      2. Pass this as a ClusterConfigSpecEx object into rulesSpec

           my $cluster_config_spec = ClusterConfigSpecEx->new(rulesSpec => [$cluster_group_spec]);

      3. Reconfigure with spec as $cluster_config_spec.

      What I am confused about is how to pass in info.vm from the PowerCLI. There is no member of info or ClusterConfigInfo called "vm". How would I tell the Group Spec what VM to use?


      Updated the title to be more informative (it is no longer solely a "translation", the thread contains the answer to the title now).

        • 1. Re: PowerCLI translation to Perl?
          stumpr Master

          That's just an array of your VM morefs for the affinity rule (or disaffine rule).


          You should be able to set your operation to "add" and then just provide an array of vm morefs that would be appended to your existing rule.


          $groupSpec = new ClusterGroupSpec();

          $groupSpec->{operation} = new ArrayUpdateOperation("add");

          $groupSpec->{info} = new ClusterVmGroup(name => $drsVmGroupName);

          $groupSpec->{info}->{vm} = [$vmMoref];


          $spec = new ClusterConfigSpecEx();

          $spec->{groupSpec} = [$groupSpec];


          $cluster->ReconfigureComputeResource_Task(spec => $spec, modify => 1);


          Check my logic on the add operation there, typed this up from memory and a few glances at the API docs.  You could also add more than one VM if you wanted to here as well (which is likely what you want to do for an affine rule).

          1 person found this helpful
          • 2. Re: PowerCLI translation to Perl?
            mcescalante Novice

            My script is basically adding a single VM to a rule.


            This is super and works (more or less), but when I use "add" as the array op it throws a SOAP Invalid Argument. When I use "edit", it removes all other machines from the rule and then ONLY adds the machine specified. Is this because I am creating a wholly new "ClusterVmGroup"?


            Either way, I'd love to figure out why it's throwing on "add", or I'm glad to just use "edit" and add it to the existing group. Cheers!

            • 3. Re: PowerCLI translation to Perl?
              stumpr Master

              You know what, get the existing ClusterVmGroup and set that to the Info for the $groupSpec instead.  That's probably the cause of the problem.  Sorry about that, answered without validating it on my end, but looking at it now it makes sense since we're telling it to add to a non-existant  ClusterVmGroup.  Using the existing one should solve the problem.  So you may need a little code to just loop through the ClusterVmGroups and find one matching a name from your script input parameter.

              • 4. Re: PowerCLI translation to Perl?
                mcescalante Novice

                This makes perfect sense, and I grabbed the old ClusterVmGroup, and used "add" as the array update option. Still getting a SOAP fault.


                When I use "edit" with the existing ClusterVmGroup it still replaces all machines in the group with the one specified.


                Here is the code that is producing the SOAP Fault:


                                my $groupSpec = new ClusterGroupSpec();

                                $groupSpec->{operation} = new ArrayUpdateOperation("add");

                                $groupSpec->{info} = $ClusterVmGroupView; #this is ONLY the ClusterVmGroup I'm editing (retrieved using configurationEx->group)

                                $groupSpec->{info}->{vm} = [$vm_view];

                                my $spec = new ClusterConfigSpecEx();

                                $spec->{groupSpec} = [$groupSpec];

                                $cluster_view->ReconfigureComputeResource(spec => $spec, modify => 1);


                And the error:

                SOAP Fault:


                Fault string: A specified parameter was not correct.

                Fault detail: InvalidArgument

                • 5. Re: PowerCLI translation to Perl?
                  stumpr Master

                  The add/edit operations are for the groups, but the group contents will just be low level array edits.


                  Take a look at the attached sample script, I tested it and it's working.  Basically the logic is the same, the difference is you edit the group, but just add the vm moref of the vm you want to add to the group by just sending in an updated array of vm morefs.


                  perl addVm2DrsGroup.pl --server= --username=administrator@vlab --password=* --cluster=CLU01 --drsgroup=group1 --vm=recovery-03

                  • 6. Re: PowerCLI translation to Perl?
                    thomasfoinLVDN Lurker



                    "When I use "edit", it removes all other machines from the rule and then ONLY adds the machine specified."

                    I have the same problem... Have you found a solution to fix this issue ?