Importing existing Virtual Machines in vCAC automatically via PowerShell

In case you would like to import existing machines into vCAC you have the option to import them via ‘Discovery’ from within the GUI. But if you wanted to import more than a few machines at a time this might become a little more than tedious. Of course you can always revert back to AutoIT or something similar to do the clicking for you, but you might be tempted to approach this in a more sophisticated way. Luckily there is an API that help you tackle that problem in a more feasible manner.

You can actually use the VMPS SOAP interface and use the ‘RegisterExistingVirtualMachine()’ method. In PowerShell this could be implemented similar to this:

# We start with a Management Context (REPO connection)
PS > $m.GetType().FullName;
DynamicOps.ManagementModel.ManagementModelEntities
PS > $m.BaseUri;
AbsolutePath   : /Repository/Data/ManagementModelEntities.svc
AbsoluteUri    : https://vcac52.sharedop.org/Repository/Data/ManagementModelEntities.svc
LocalPath      : /Repository/Data/ManagementModelEntities.svc
Authority      : vcac52.sharedop.org
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /Repository/Data/ManagementModelEntities.svc
Segments       : {/, Repository/, Data/, ManagementModelEntities.svc}
IsUnc          : False
Host           : vcac52.sharedop.org
Port           : 443
Query          :
Fragment       :
Scheme         : https
OriginalString : https://vcac52.sharedop.org/Repository/Data/ManagementModelEntities.svc
DnsSafeHost    : vcac52.sharedop.org
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

# Then we instantiate our web service ...
PS > $Service = 'VMPS';
PS > [Uri] $UriVMPS = '{0}://{1}:{2}/{3}' -f 
  $m.BaseUri.Scheme, $m.BaseUri.Host, $m.BaseUri.Port, $Service;
PS > $VMPS = New-WebServiceProxy $UriVMPS -Class $Service -Namespace $Service;

# ... and get the definition for the RegisterExistingVirtualMachine method
PS > $vmps.RegisterExistingVirtualMachine;
OverloadDefinitions
-------------------
void RegisterExistingVirtualMachine(
  VMPS.IdentityToken securityToken, 
  string virtualMachineID, 
  string hostReservationID, 
  string hostStorageReservationID, 
  string virtualMachineTemplateID, 
  string owner
  )

# Pick a machine to import/register
PS > $Machine = $m.VirtualMachines |? VirtualMachineName -eq 'ExistingServer1';
PS > $Machine | select VirtualMachineName, VirtualMachineID, VMCPUs, 
  VMTotalMemoryMB, VMTotalStorageGB;
VirtualMachineName : ExistingServer1
VirtualMachineID   : c4ef69ab-e4fb-4f56-9e28-02a6f2b82cde
VMCPUs             : 1
VMTotalMemoryMB    : 0
VMTotalStorageGB   : 2

# Let's use current user for token and owner
PS > $IdTkn = New-Object VMPS.IdentityToken;
PS > $Username = '{0}\{1}' -f $ENV:USERDOMAIN, $ENV:USERNAME;
PS > $OwnerName = $Username;
PS > $IdTkn.User = $Username;

# Then we choose our target resources where to import ...
# 1. Blueprint
PS > $m.VirtualMachineTemplates | select 
  VirtualMachineTemplateName, VirtualMachineTemplateID;
VirtualMachineTemplateName VirtualMachineTemplateID
-------------------------- ------------------------
BP03YAST                   e3011d25-f23e-4f79-a9a2-3bbbb6706cfc
WebServer                  b6dc1746-31b1-49bd-ab77-9d760c420a4d
BP02                       a4a38b9a-c874-462c-8f88-e3b70e72da3b
BP01                       b9fe831b-26fd-4ef0-b5c2-ee8f258f1727
PS > $bp = $m.VirtualMachineTemplates |? VirtualMachineTemplateName -eq 'BP01';

# 2. HostReservation
PS > $m.HostReservations | select HostReservationID, HostReservationName
HostReservationID                    HostReservationName
-----------------                    -------------------
93d0987d-1a3e-4ebf-8564-164a6bf985f4 GOLD01
072e376f-d56f-483b-ae1a-2d3c26ee2c2c BRONZE01
74ddaaed-7685-4848-9cea-4e82b29ca8e5 SILVER01
8dfd1c82-f9b6-4c43-8aaa-c76b95418525 GOLD02
PS > $hr = $m.HostReservations |? HostReservationName -eq 'GOLD';

# 3. StorageReservation
PS > $ahrts = $m.LoadProperty($hr, 'HostReservationToStorages') | 
  Select HostReservationToStorageID, Enabled, ReservationPriority |? 
  Enabled -eq $True;
PS > $ahrts
HostReservationToStorageID           Enabled ReservationPriority
--------------------------           ------- -------------------
8f81e3c5-13b6-41b0-ba2b-4d1b8f0552ee True                      2
17de5a1c-98b3-4832-97fc-6787861a19da True                      0
8536128e-3e08-4564-9405-ea191a3c5bbe True                      8
PS > $srid = $ahrts | sort -Property ReservationPriority | 
  Select HostReservationToStorageID -First 1;

# Submit our request to register the existing machine
PS > $VMPS.RegisterExistingVirtualMachine($IdTkn, $Machine.VirtualMachineID, 
  $hr.HostReservationID, $srid.HostReservationToStorageID, 
  $bp.VirtualMachineTemplateID, $OwnerName);

The corresponding SOAP payload will then look similar to this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <RegisterExistingVirtualMachine xmlns="http://dynamicops.com/VMPS">
      <securityToken>
        <ImpersonatingUser xsi:nil="true" xmlns="http://DynamicOps.VMPS.Model" />
        <User xmlns="http://DynamicOps.VMPS.Model">SHAREDOP.ORG\vcacservice</User>
      </securityToken>
      <virtualMachineID>c4ef69ab-e4fb-4f56-9e28-02a6f2b82cde</virtualMachineID>
      <hostReservationID>93d0987d-1a3e-4ebf-8564-164a6bf985f4</hostReservationID>
      <hostStorageReservationID>17de5a1c-98b3-4832-97fc-6787861a19da</hostStorageReservationID>
      <virtualMachineTemplateID>b9fe831b-26fd-4ef0-b5c2-ee8f258f1727</virtualMachineTemplateID>
      <owner>SHAREDOP.ORG\vcacservice</owner>
    </RegisterExistingVirtualMachine>
  </soap:Body>
</soap:Envelope>

Comments

  1. Giampiero De Ciantis says:

    Is this still available in vcac 6?

    • Ronald Rink says:

      I actually have not tested this with vCAC 6 as there is a utility from GSS that does exactly the same thing and this is also available for vCAC 6. However, this tool is not public, but you would have to request it via GSS or PSO.

  2. praveen says:

    I tried to import existing Virtual Machines in vCAC . For this I used powershell script as mentioned above and got an exception :
    Exception calling “RegisterExistingVirtualMachine” with “6” argument(s): “An error occurred when verifying security for the message.”
    +$VMPS.RegisterExistingVirtualMachine <<<< ($IdTkn, $Machine.VirtualMachineID,
    $hr.HostReservationID, $srid.HostReservationToStorageID,
    $bp.VirtualMachineTemplateID, $OwnerName);
    +CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    +FullyQuilifiedErrorId : DotNetMethodException

    I logged in as administrator account. Can you please tell me, How to solve this?
    Thanks,
    PK

    • Ronald Rink says:

      Hi PK, I haven’t actually seen this error before, so I cannot really tell you how this is solved. However I would start a web proxy debugger like Fiddler to see the actual SOAP request and (more important) the SOAP response from vCAC. This might tell you more about the actual error and help you solve it. Maybe you can send the trace to me so I could have a look at it. Regards, Ronald

Trackbacks

  1. […] you can execute. To further dive into it you can have a look at some of my other blog posts like machine import and Approval/Reject of requested machines. Here is a list of all actions (excluding all async […]

  2. […] my research regarding the import of existing virtual machines I came across the ‘RegisterExistingVirtualMachine()’ SOAP action the VMPS […]

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

%d bloggers like this: