VMware {code} Community
Manoj_Shingare
Contributor
Contributor
Jump to solution

C# code for Creating a virtual machine

How to create virtual machine using vi sdk 2.5 and virtual center server 2.5 in C#?

I will really appreciate if any c# sample code is available .

Thanks in advance for help.

0 Kudos
1 Solution

Accepted Solutions
njain
Expert
Expert
Jump to solution

Hi Manoj,

The reason that you see the error is because the SCSI Controller added to the Virtual Machine has a "physical" bus sharing. If you change this setting so that it is not shared between virtual machines, you will be able to power on your VM.

In the above code sample, if you edit the following line:

scsiCtrl.sharedBus = VirtualSCSISharing.physicalSharing;

to

scsiCtrl.sharedBus = VirtualSCSISharing.noSharing;

it should work for you.

Regards,

Neha

View solution in original post

0 Kudos
18 Replies
njain
Expert
Expert
Jump to solution

Hi Manoj,

You can use the "CreateVM_Task" method available in VI API to create a new Virtual Machine. For your reference, attached is a C# sample that creates a Virtual Machine in VC. This sample takes in the following arguments:

1. VirtualCenter IP

2. Username

3. Password

4. Datacenter Name

5. Host name

6. New Virtual Machine name

You can modify the config specification of the Virtual Machine in the sample to suit up to your needs.

Hope the above information is helpful.

Regards,

Neha

0 Kudos
Manoj_Shingare
Contributor
Contributor
Jump to solution

Hi Neha,

Thnx for help .

I executed the code which u have given . It successfully creates the new virtual machine on specified host .

But I am not able to power on this newly created machine . I got the fallowing error when I tried to do so through Virtual Center client.

" VMware Esx Server can not open the virtual disk ...... .... .... .... for clustering . Please verify that the virtual disk was created using 'Thick' option ....." .

Could you tell me, what was the reason for this failure. I am new in this kind of development . I will really very thankful for your help.

0 Kudos
njain
Expert
Expert
Jump to solution

Hi Manoj,

The reason that you see the error is because the SCSI Controller added to the Virtual Machine has a "physical" bus sharing. If you change this setting so that it is not shared between virtual machines, you will be able to power on your VM.

In the above code sample, if you edit the following line:

scsiCtrl.sharedBus = VirtualSCSISharing.physicalSharing;

to

scsiCtrl.sharedBus = VirtualSCSISharing.noSharing;

it should work for you.

Regards,

Neha

0 Kudos
Manoj_Shingare
Contributor
Contributor
Jump to solution

Hi Neha,

Its working fine.

Thnx for such quick reply.

0 Kudos
Hakime
Contributor
Contributor
Jump to solution

Hello Neha.

I was going through the createVM.cs codes.

I was wondering if the cert policy is really necessary.

If they are,what cert are they from?

Maybe from the ESX/VC certs maybe?

And also,in the createVMTask void..There are codes for user ID,password,datacenter name & stuff.

Can you tell me what are they about?

I'm currently doing my project on virtualization and I don't have access to ESX/VC servers.

Is it possible to create/Destroy/Power On VMs with just VI client & some C# codes?

Thanks for your time & help.

0 Kudos
Niket
Enthusiast
Enthusiast
Jump to solution

Hi,

CertPolicy is neccessary to validate the server certificate and in absence of this any issue related with certificate would not be catered by the code any connection can not be made.

The CreateVMTask method take these argument as a parameter to connect to ESX/VC and create a VM in the specified host under the datacenter.

You can perform all the operation using C# code whatever you can execute using VI Cleint.

Thanks

Niket

0 Kudos
Hakime
Contributor
Contributor
Jump to solution

Hi Niket.

Thanks for your quick reply.

I was wondering.I don't have an esx server.Only the VI client.This was preinstalled before I use this comp.

Do the VS server come with the VI client?

Thanks

Hakim

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi Hakim,

VI Client is just a GUI to manage both ESX and Virtual Center. The VI Client comes with both of them. You need to purchase both ESX and Virtual Center. Virtual Center is a server which can manage number of hosts (ESX boxes) together along with various features as Cloning, templates, HA, DRS and VMotion. Please refer Basic Administration Guide available at http://www.vmware.com/pdf/vi3_35/esx_3/r35/vi3_35_25_admin_guide.pdf for details on how you can leverage features using VI Client.

- Seemanki

0 Kudos
Hakime
Contributor
Contributor
Jump to solution

Thanks for helping me out a lot Seemankij. 😃

0 Kudos
ghosther
Contributor
Contributor
Jump to solution

Please somebody...

Could you tell me all requeriments for develop a C# application that execute the following usin Virtual Center:

Power On a Vm

Power Off

Get Performace Status(like web admin in ESX Server)

i need to know all the software that i may install on my pc...

i have installed:

Visual Studio(off corse)

Esx server 2.0

And nothing else...

please could you give me the rest of the list, and the url??

and a sample application code.

Regards.

0 Kudos
KobAl
Contributor
Contributor
Jump to solution

Hello,

I also would like to use this example for creating a new VirtualMachine.

The Problem for me is, that we dont have a Datacenter because we just have one ESX.

Is it able to recode this example without giving a "Datacenter Name" ??

I hope you are able to help me, because im really pressurized.

If its not able to recode it, you would help me to give a sample code for creating a new VM with using the VMware.Vim. (We are basically using this)

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi,

There's always a datacenter associated with an ESX server and by default it is named as "ha-datacenter". For a vCenter Server, the datacenter name changes to the name provided by user. Hence, you can invoke the CreateVM.cs sample by passing the ESX server IP and the datacenter as ha-datacenter. You can also verify the datacenter name using the MOB (managed object browser), which is a web-based server application hosted on all ESX Server and VirtualCenter Server systems. It lets you explore the objects on the system and obtain information about available properties and methods. To access the MOB on any ESX Server or VirtualCenter Management Server system:

1. Launch a browser.

2. Enter the fully-qualified domain name (or the IP address) for the ESX Server host or VirtualCenter Management Server:

https:///mob

I hope this addresses your concern!

Thanks,

Angela

0 Kudos
KobAl
Contributor
Contributor
Jump to solution

WOW yea, you helped me a lot.

Now im able to create a VM with using the VimApi, the Problem is now how to create a VM with using the Vmware.Vim?

Many methods or properties are hard to get, additionally i really want the "minimal requirement" to create a VM.

I dont know what is really requiered, maybe you can help me there.

regrads

0 Kudos
yercaudARUN
Contributor
Contributor
Jump to solution

Hi,

I am able to create the VM after changing the

" scsiCtrl.sharedBus = VirtualSCSISharing.noSharing;"

When i Power on th VM i get an error in the console stating the hardware config doesnt support OS installation.

the config is as per the given code and the changes made is for HDD size to 8 GB. i am trying to install an XP 32 bit OS

How do i debug the problem.

In case if i power on the VM through code and the H/w config is not suitable for tat OS installation how do i capture these errors.

Please let me know..

Regards,

Arun

0 Kudos
yercaudARUN
Contributor
Contributor
Jump to solution

Hi,

I was able to create the Virtual Machine for IDE successfully,

How do i get the reference of the created virtual machine to power in On.

Could you also guide me in the capturing of H/W configuration errors tat occur during the Power On.

Thanks in Advance,

Arun.M

0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi Arun,

There are couple of ways to fetch the reference of the VM. One of them is by invoking the FindByInventoryPath API, by passing the VM inventory path. When you have the VM MOR, you can invoke the PowerOnVM API on this MOR to power on the VM. If the VM fails to power on , a fault is thrown which one can capture to analyze the same.

I would request to please initiate a new thread for your further queries, since it is best to have one issue per thread. That way it is easier for the community users to respond Smiley Happy

0 Kudos
MohdAsif
Contributor
Contributor
Jump to solution

Hi,

can you explain How can i add existing vmdk files while creation of vm using createvm class?

can you tell me how to add guest os iso image /vmdk files to newly created vm using c# code?

Thanks in advance.

0 Kudos
MohdAsif
Contributor
Contributor
Jump to solution

can you explain How can i add existing vmdk files while creation of vm using createvm class?

can you tell me how to add guest os iso image /vmdk files to newly created vm using c# code?

Thanks in advance.

0 Kudos