VMware Cloud Community
wwstreet
Enthusiast
Enthusiast

Event Broker - Running a PS script upon destroying a server

I am trying to run a small PS script to remove a server from AD and DNS but only when the server is destroyed.  When I looked through the event topics, I can't really find anything to act on when a server is destroyed.  Has anyone else had success with this?

10 Replies
chris_scoffield
Contributor
Contributor

We have a vRO workflow doing similar things fired from an event.  The subscription rules we use are

Data-> LifeCycle > State Name = VMPSMasterWorkflow32.Disposing

Data->Lifecycle > State Phase = POST

Data > Machine > Machine Type = Virtual Machine

Data -> Blueprint Name Contains some string that matched our windows  blueprints


rmav01
Enthusiast
Enthusiast

Got something similar to Chris' on our instance as well. In case you are wondering the event topic is com.vmware.csp.iaas.blueprint.service.machine.lifecycle.provision.

0 Kudos
wwstreet
Enthusiast
Enthusiast

Chris's answer kicks off my script, however it kicks it off after the system has been destroyed so I can't pass the IP and name to my script to remove it from DNS.  I normally pull the VM's properties via payload and hash what I need out of that.  Not sure of any other way to do it.

0 Kudos
rmav01
Enthusiast
Enthusiast

There are other lifecycle states. Try VMPSMasterWorkflow32.UnprovisionMachine. Also, try state phase PRE.

0 Kudos
wwstreet
Enthusiast
Enthusiast

PRE works, however this is all I am getting from the server I am removing.  I normally pull the name and IP from the properties listed further below.  However these properties are not coming across correctly so I can no longer pull my IP and Name to send back to the DNS server to delete.

2016-08-17 11:28:59.658] [I] BlueprintName: _Software Testing - W2K8 BP

[2016-08-17 11:28:59.661] [I] ComponentId: tier1w2k8s

[2016-08-17 11:28:59.662] [I] ComponentTypeId: Infrastructure.CatalogItem.Machine.Virtual.vSphere

[2016-08-17 11:28:59.664] [I] EndpointId: c39838f0-76b8-45a2-872a-b80b7b511436

[2016-08-17 11:28:59.665] [I] RequestId: 47f70d76-b6a3-4fbb-a7ef-ea940c003022

[2016-08-17 11:28:59.666] [I] VirtualMachineEvent: null

[2016-08-17 11:28:59.668] [I] WorkflowNextState: null

[2016-08-17 11:28:59.669] [I] State: VMPSMasterWorkflow32.Disposing

[2016-08-17 11:28:59.670] [I] Phase: PRE

[2016-08-17 11:28:59.671] [I] Event: null

[2016-08-17 11:28:59.673] [I] ID: a57487cf-d39a-4c2a-8854-e1a2093abae2

[2016-08-17 11:28:59.674] [I] Name: wstinfsql04

[2016-08-17 11:28:59.675] [I] ExternalReference: vm-12458

[2016-08-17 11:28:59.677] [I] Owner: WWStreet@basspro.net

[2016-08-17 11:28:59.678] [I] Type: 0

[2016-08-17 11:28:59.679] [I] Properties: null

[2016-08-17 11:28:59.681] [I] vcName: null

[2016-08-17 11:28:59.682] [I] tstpTime: null

[2016-08-17 11:28:59.684] [E] Error in (Workflow:Remove a VM from BPS DNS / Scriptable task (item1)#41) TypeError: Cannot call method "get" of null

[2016-08-17 11:28:59.695] [E] Workfow execution stack:

***

item: 'Remove a VM from BPS DNS/item1', state: 'failed', business state: 'null', exception: 'TypeError: Cannot call method "get" of null (Workflow:Remove a VM from BPS DNS / Scriptable task (item1)#41)'

workflow: 'Remove a VM from BPS DNS' (95a78788-b279-4fba-9988-ef3cb4a0ad78)

|  'attribute': name=host type=PowerShell:PowerShellHost value=dunes://service.dunes.ch/CustomSDKObject?id='8dac6014-29ad-4148-ad6d-d5c35180a726'&dunesName='PowerShell:PowerShellHost'

|  'attribute': name=externalScript type=string value=C:\scripts\BPSDNSDelete.ps1

|  'attribute': name=arguments type=string value=

|  'input': name=payload type=Properties value=#[#lifecycleState#=#Properties##[#phase#=#string#PRE#+#state#=#string#VMPSMasterWorkflow32.Disposing#]##+#componentId#=#string#tier1w2k8s#+#blueprintName#=#string#_Software Testing - W2K8 BP#+#componentTypeId#=#string#Infrastructure.CatalogItem.Machine.Virtual.vSphere#+#IaaSTimeoutId#=#number#8643.0#+#endpointId#=#string#c39838f0-76b8-45a2-872a-b80b7b511436#+#machine#=#Properties##[#externalReference#=#string#vm-12458#+#owner#=#string#WWStreet@basspro.net#+#name#=#string#wstinfsql04#+#id#=#string#a57487cf-d39a-4c2a-8854-e1a2093abae2#+#type#=#number#0.0#]##+#requestId#=#string#47f70d76-b6a3-4fbb-a7ef-ea940c003022#]#

|  'output': name=output type=PowerShell:PowerShellRemotePSObject value=null

|  'attribute': name=__asd_requestedBy type=string value=WWStreet@basspro.net

|  'attribute': name=__asd_tenantRef type=string value=vsphere.local

|  'attribute': name=__asd_targetResourceProviderTypeId type=string value=com.vmware.csp.iaas.blueprint.service

|  'attribute': name=__asd_requestInstanceId type=string value=aff9ecd0-6497-11e6-01ac-0f7648af61b7

|  'attribute': name=__asd_requestInstanceTypeId type=string value=com.vmware.csp.iaas.blueprint.service.machine.lifecycle.provision

|  'attribute': name=__asd_catalogRequestId type=string value=c1f91f2e-e019-413e-8fec-d4ca6e0e456c

|  'attribute': name=__asd_requestedFor type=string value=WWStreet@basspro.net

|  'attribute': name=__asd_targetResourceId type=string value=a57487cf-d39a-4c2a-8854-e1a2093abae2

|  'attribute': name=__asd_requestInstanceTimestamp type=string value="2016-08-17T11:28:53.000-05:00"

|  'attribute': name=__asd_targetResourceTypeId type=string value=machine

|  'attribute': name=__asd_correlationId type=string value=aff9ecd0-6497-11e6-01ac-0f7648af61b7

|  'attribute': name=__asd_requestTraceId type=string value=nLvspQuk

|  'attribute': name=__asd_targetResourceProviderId type=string value=009c605a-3f3c-440a-9c56-bd6397aa0b9d

*** End of execution stack.

_____________________________________________________________________________________________________________________________________________________

When I add my DNS.

[2016-08-16 13:59:35.440] [I] BlueprintName: _Software Testing - W2K8 BP

[2016-08-16 13:59:35.443] [I] ComponentId: tier1w2k8s

[2016-08-16 13:59:35.444] [I] ComponentTypeId: Infrastructure.CatalogItem.Machine.Virtual.vSphere

[2016-08-16 13:59:35.445] [I] EndpointId: c39838f0-76b8-45a2-872a-b80b7b511436

[2016-08-16 13:59:35.447] [I] RequestId: 52bb0d7c-aca2-4517-adf2-d9ddb56723a7

[2016-08-16 13:59:35.448] [I] VirtualMachineEvent: null

[2016-08-16 13:59:35.449] [I] WorkflowNextState: null

[2016-08-16 13:59:35.451] [I] State: VMPSMasterWorkflow32.MachineProvisioned

[2016-08-16 13:59:35.452] [I] Phase: POST

[2016-08-16 13:59:35.453] [I] Event: null

[2016-08-16 13:59:35.454] [I] ID: 600b6a9f-3067-42b4-8f49-27e1b0ce15e7

[2016-08-16 13:59:35.456] [I] Name: wstinfsql03

[2016-08-16 13:59:35.457] [I] ExternalReference: vm-12333

[2016-08-16 13:59:35.458] [I] Owner: WWStreet@basspro.net

[2016-08-16 13:59:35.460] [I] Type: 0

[2016-08-16 13:59:35.461] [I] Properties: HashMap:575755251

[2016-08-16 13:59:35.462] [I] vcName: null

[2016-08-16 13:59:35.464] [I] tstpTime: null

[2016-08-16 13:59:35.465] [I] vCAC VM properties :

   Cafe.Shim.VirtualMachine.TotalStorageSize : 50

   Custom.ComponentMachine.HostnameScheme : {bps.os}{bps.env}{bpsApp}{bps.mw}{bps.srvrfunct}{##}

   Custom.ComponentMachine.NoIndexOnFirst : false

   Custom.Deployment.HostnameScheme :

   Custom.Deployment.NoIndexOnFirst : true

   Custom.Hostname.OwnerShortNameIdentifier : USR

   Custom.SetCustomHostname.Execute : true

   Extensibility.Lifecycle.Properties.VMPSMasterWorkflow32.MachineProvisioned : *

   Extensibility.Lifecycle.Properties.VMPSMasterWorkflow32.Requested : *

   Hostname : wstinfsql03

   VirtualMachine.Admin.AgentID : c6cf1f42-4a22-1c9d-5f3e-b0c6ddeae928

   VirtualMachine.Admin.TotalDiskUsage : 51200

   VirtualMachine.Admin.UUID : 501f8872-18ba-a6e7-e6ba-3693092a8999

   VirtualMachine.CPU.Count : 1

   VirtualMachine.Cafe.Blueprint.Component.Cluster.Index : 0

   VirtualMachine.Cafe.Blueprint.Component.Id : tier1w2k8s

   VirtualMachine.Cafe.Blueprint.Component.TypeId : Infrastructure.CatalogItem.Machine.Virtual.vSphere

   VirtualMachine.Cafe.Blueprint.Id : W2K8SoftwareTesting

   VirtualMachine.Cafe.Blueprint.Name : _Software Testing - W2K8 BP

   VirtualMachine.Disk0.DeviceID : SCSI (0:0)

   VirtualMachine.Disk0.ExternalID : 6000C29f-63a1-3205-95cd-de5efa1931e0

   VirtualMachine.Disk0.IsClone : true

   VirtualMachine.Disk0.IsFixed : False

   VirtualMachine.Disk0.Label : Hard disk 1

   VirtualMachine.Disk0.Name : Hard disk 1

   VirtualMachine.Disk0.Size : 50

   VirtualMachine.Disk0.Storage : PROD_DATASTORE_CLUSTER_FTW_LOCAL

   VirtualMachine.Disk0.Storage.Cluster.ExternalReferenceId : group-p3288

   VirtualMachine.Disk0.Storage.Cluster.Name : PROD_DATASTORE_CLUSTER_FTW_LOCAL

   VirtualMachine.Memory.Size : 4096

   VirtualMachine.Network0.Address : 172.24.74.23

   VirtualMachine.Network0.AddressType : Static

   VirtualMachine.Network0.DnsSearchSuffixes : basspro.net

   VirtualMachine.Network0.DnsSuffix : basspro.net

   VirtualMachine.Network0.Gateway : 172.24.74.1

   VirtualMachine.Network0.MacAddress : 00:50:56:9f:4b:34

   VirtualMachine.Network0.Name : INT-VRA-PROD_VLAN74

   VirtualMachine.Network0.NetworkProfileName : vRA Prod

   VirtualMachine.Network0.PrimaryDns : 172.24.37.101

   VirtualMachine.Network0.PrimaryWins :

   VirtualMachine.Network0.SecondaryDns : 10.33.80.103

   VirtualMachine.Network0.SecondaryWins :

   VirtualMachine.Network0.SubnetMask : 255.255.255.0

   VirtualMachine.Storage.Cluster.Automation.Behavior : manual

   VirtualMachine.Storage.Cluster.Automation.Enabled : True

   VirtualMachine.Storage.Cluster.ExternalReferenceId : group-p3288

   VirtualMachine.Storage.Cluster.Name : PROD_DATASTORE_CLUSTER_FTW_LOCAL

   VirtualMachine.Storage.Name : PROD_DATASTORE_CLUSTER_FTW_LOCAL

   Vrm.ProxyAgent.Uri : https://itmgma.basspro.net/VMPS2Proxy

   __Cafe.Request.BlueprintType : 1

   __Cafe.Request.VM.ArchiveDays : 45

   __Cafe.Request.VM.HostnamePrefix : SRV

   __Cafe.Request.VM.LeaseDays : 1

   __Cafe.Root.Request.Id : 1b24ce9d-1326-440d-ae4b-275c946a3e37

   __Clone_Type : CloneWorkflow

   __DailyCost : {"type":"moneyTimeRate","cost":{"type":"money","currencyCode":"USD","amount":1.39},"basis":{"type":"timeSpan","unit":4,"amount":1}}

   __InterfaceType : vSphere

   __Legacy.Workflow.ImpersonatingUser :

   __Legacy.Workflow.User : WWStreet@basspro.net

   __Notes : Tier 1

Windows 2008 R2 Standard

   __VirtualMachine.Allocation.InitialMachineState : SubmittingRequest

   __VirtualMachine.ProvisioningWorkflowName : CloneWorkflow

   __api.request.callback.service.id : 260b0a87-4c85-46d9-ac51-c6e74463ca11

   __api.request.id : 52bb0d7c-aca2-4517-adf2-d9ddb56723a7

   __clonefrom : TMPL_W2K8 Standard

   __clonespec : vRA

   __iaas_request_binding_id : 76e6c5cc-79e3-45a6-869a-d3c09c36dda9

   __request_reason :

   __reservationPolicyID : 7956ac63-f0e5-4d92-826d-0ba81535c61d

   __trace_id : lcr6ZgIE

   _number_of_instances : 1

   bps.dept : it

   bps.env : t

   bps.mw : sql

   bps.os : ws

   bps.srvrfunct :

   bpsApp : inf

   osfamily : Windows

   tApp :

   tPatchDay : p.Wednesday

   tStakeHolder : IT.Server.Windows

   tTier : 'Tier 2'

   targetVM :

   tmDay : m.Wednesday

   tstpTime : mstp.1500

   tstrtTime : mstrt.1500

   vcServer : srvvcsitftw00.basspro.net

[2016-08-16 13:59:40.044] [I]

Add A Record for wstinfsql03.basspro.net at basspro.net

Command completed successfully.

0 Kudos
rmav01
Enthusiast
Enthusiast

I'm a little fuzzy on the lifecycle orderings. Is there any change if you triggered the event during VMPSMasterWorkflow32.DeactivateMachine instead of disposing?

0 Kudos
GrantOrchardVMw
Commander
Commander

Deactivate is for lease expiration. If you have an archive period of 0 that is not a problem, but failed requests won't trigger it either.

There are workflows for this OOTB with vRO, is there a reason to do this with POSH instead?

Grant http://grantorchard.com
0 Kudos
wwstreet
Enthusiast
Enthusiast

Grant, I have not found anything in vRO that would allow for me to add and remove servers from a secure DNS.  Do you have any information on the process your speaking about?

0 Kudos
GrantOrchardVMw
Commander
Commander

You're right, I was thinking of the AD computer cleanup.

For what you are doing I make use of the "run script in guest" vRO workflow, and have the POSH execute from the Web/Manager server, not on the machine that is being destroyed.

Grant http://grantorchard.com
0 Kudos
wwstreet
Enthusiast
Enthusiast

Unfortunately, while I can kick the workflow off, I am not getting the name and IP of the VM to pass into my script.

0 Kudos