Is it possible to use a workflow or write a script which create a computer object in the active directory in a specific OU when add the vm to the domain?
I prefer not to create a new object in the AD before adding the vm to the domain.
What is the best way to do it?
I can see this is an older thread but I don't see a resolution. What I did was combine the following workflows: Create a computer in an organizational unit and Clone thin provisioned, Windows with single NIC and credential. I also added a bit of customization to limit the input options, send email notifications and require approvals through a boolean option but the results worked well. What I still haven't figured out and would like to is how to inject service account credentials into the initial domain join.
This doesn't completely meet your requirements as it does technically create the AD object prior to adding to the domain but it is all still automated in the same workflow.
Look at Microsoft - > AD plugin. You can add domains with this too. What I did to solve this problem was use the AD plugin to register any domain that I knew I would add computer objects to. Once you have the domain registered you can see the tree structure under the inventory tab. Then using the AD plugin use the create computer object and reference the the domain objects. I had some condition that would select the correct domain based on input parameters and then populate the domain to add the computer object to.
As far as joining the domain. The default workflow for cloning windows servers should have a domain and domain credentials input fields that you can populate to join the domain after the clone happens. If you create the computer object prior to the the clone the domain should move the computer object into the correct OU as soon as it joins the domain.