Change Ownership of a vCAC Machine and Inform user via mail with PowerShell

When you want to change the owner of a machine in vCAC you would probably like to inform the owner of what he just acquired. The easist thing is to to send an update notification via vCACs VMPS SOAP (ProvisionService) service:

# What we need
# 1. a connection to the ProvisionService
PS > $VMPS
SoapVersion                          : Default
AllowAutoRedirect                    : False
CookieContainer                      :
ClientCertificates                   : {}
EnableDecompression                  : False
UserAgent                            : Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.18063)
Proxy                                :
UnsafeAuthenticatedConnectionSharing : False
Credentials                          :
UseDefaultCredentials                : False
ConnectionGroupName                  :
PreAuthenticate                      : False
Url                                  : https://vcac52.sharedop.org/VMPS
RequestEncoding                      :
Timeout                              : 100000
Site                                 :
Container                            :

PS > $VMPS.GetType();
IsPublic IsSerial Name             BaseType
-------- -------- ----             --------
True     False    ProvisionService System.Web.Services.Protocols.SoapHttpClientProtocol

# 2. Management Context to repository service
PS > $m.GetType().FullName
DynamicOps.ManagementModel.ManagementModelEntities

# 3. The machine for which we change the owner information
PS > $Machine.GetType().FullName
DynamicOps.ManagementModel.VirtualMachine
PS > $Machine | Select VirtualMachineID, VirtualMachineName;
VirtualMachineID                     VirtualMachineName
----------------                     ------------------
63574612-ecf0-46c2-809f-d580f55e2f26 vc100277

# 4. The new user
PS > $Username = 'SHAREDOP\Edgar.Schnittenfittich;

# Load user object of new owner
PS > $u = $m.Users |? UserName -eq $Username;

# Set link to new owner and save changes
PS > $m.SetLink($Machine, 'Owner', $u);
PS > $m.UpdateObject($Machine);
PS > $m.SaveChanges();
Descriptor                        Headers                           StatusCode Error
----------                        -------                           ---------- -----
System.Data.Services.Client.Li... {[DataServiceVersion, 1.0;], [... 204
System.Data.Services.Client.En... {[DataServiceVersion, 1.0;], [... 204

# Notify new owner, use a user that can actually trigger the notification as the vcac service account
PS > $idTkn = New-Object VMPS.IdentityToken;
PS > $idTkn.User = '{0}\{1}' -f $ENV:USERDOMAIN, $ENV:USERNAME;
# You can ignore the error message or wrap it into a try/catch block. 
# The email will be sent to the email address of the user.
PS > $VMPS.TriggerUpdateMachineOwnerEvents($idTkn, $Machine.VirtualMachineID);
Exception calling "TriggerUpdateMachineOwnerEvents" with "2" argument(s): 
"Event Queue operation failed with MessageQueueErrorCode QueueNotFound for queue 'UpdateOwner'."
At line:1 char:1
+ $VMPS.TriggerUpdateMachineOwnerEvents($idTkn, $VirtualMachineID)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapException

You can ignore the error message and wrap it into a try/catch block to hide it. If you get a result with ‘Access Denied’ this means that the user in the IdentityToken is not allowed to trigger the event:

Exception calling "TriggerUpdateMachineNotesEvents" with "2" argument(s): "Access Denied"
At line:1 char:1
+ $VMPS.TriggerUpdateMachineNotesEvents($idTkn, $VirtualMachineID)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SoapException

The ‘regular’ full error would look like this (as can be examined with Fiddler):

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
  <s:Fault>
    <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
    <faultstring xml:lang="en-US">Event Queue operation failed with MessageQueueErrorCode QueueNotFound for queue 'UpdateOwner'.</faultstring>
    <detail>
    <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <HelpLink i:nil="true"/>
      <InnerException i:nil="true"/>
      <Message>Event Queue operation failed with MessageQueueErrorCode QueueNotFound for queue 'UpdateOwner'.</Message>
      <StackTrace> at System.Workflow.Runtime.WorkflowQueuingService.EnqueueEvent(IComparable queueName, Object item)
 at System.Workflow.Runtime.WorkflowExecutor.EnqueueItem(IComparable queueName, Object item, IPendingWork pendingWork, Object workItem)
 at System.Workflow.Runtime.WorkflowInstance.EnqueueItem(IComparable queueName, Object item, IPendingWork pendingWork, Object workItem)
 at DynamicOps.VMPS.Service.VirtualMachineService.FireVirtualMachineEvent(Guid virtualMachineID, String eventName, Object data, Boolean synchronous)
 at DynamicOps.VMPS.Service.ProvisionService.FireVirtualMachineEvent(IdentityToken identityToken, Guid virtualMachineID, String eventName, Object data)
 at DynamicOps.VMPS.Service.ProvisionService.TriggerUpdateMachineOwnerEvents(IdentityToken identityToken, Guid machineId)
 at SyncInvokeTriggerUpdateMachineOwnerEvents(Object , Object[] , Object[] )
 at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
 at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
 at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace>
      <Type>System.Workflow.Runtime.QueueException</Type>
    </ExceptionDetail>
    </detail>
  </s:Fault>
</s:Body>
</s:Envelope>

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: