Hello,
Main-Problem:
I need to dev a Webpage to create VMs in our vSphere. For Info: In the enviroment we have multiple clusters with multiple hosts with many VMs.
We alrady have the following Powershell-script, but I need to use C# because I will create the new VM directly from the page.
POWERSHELL:
Add-PSSnapin VMware.VimAutomation.Core
$a = Connect-VIServer -Server $VM_Server -Protocol https -User $VM_Username -Password $VM_Password
# Create VM
$newVM = new-vm `
-name $newVmName `
-ResourcePool $VM_RessourcePool `
-Datastore $VM_Datastore `
-DiskGB $VM_DiskGB `
-diskstorageformat EagerZeroedThick `
-MemoryGB $VM_MemoryGB `
-NumCpu $VM_NumCpu `
-CD `
-GuestID $VM_GuestID `
-NetworkName $VM_NetworkName `
-Location $VM_Location `
-Notes $VM_Notes
$mac $newVM_NetworkAdapter.MacAddress
# [...]
# Some Configurations like
# - NetworkAdatper = VMXNET3
# - Bootorder: 1. NetworkAdapter, 2. CDRom, 3. HDD
# - VideoRamSize
# [...]
Well, this is what i wrote, but it wont work. I have try to use only the parameters, that it will be used in the Powershell-Script.
C#:
using VMware.Vim;
namespace TEST
{
public classVmware
{
public static void TestCreateVm()
{
var client = new VimClientImpl();
client.Login(VM_Server, VM_Username, VM_Password);
var folderFilter = new NameValueCollection {{"name", VM_Location}};
var folder = (Folder)client.FindEntityView(typeof(Folder), null, folderFilter, null);
var pool = new ManagedObjectReference(VM_RessourcePool);
var config = new VirtualMachineConfigSpec();
config.Name = newVmName;
config.MemoryMB = VM_MemoryGB*1024;
config.NumCPUs = VM_NumCpu;
var disk = new VirtualDisk {CapacityInKB = VM_DiskGB*1024*1024};
var deviceSpec = new[] { new VirtualDeviceConfigSpec() };
deviceSpec[0].Operation = VirtualDeviceConfigSpecOperation.add;
deviceSpec[0].Device = disk;
config.DeviceChange = deviceSpec;
config.GuestId = VM_GuestID;
folder.CreateVM(config, pool, null);
client.Logout();
}}}
The Exception-Message is that "a specific parameter was not correct: config.files".
I can not imagine that i have to specify the config.files parameter because I dont need to specify in the powershell command.
I think this exception is generated becaus I dont set the datastore-parameter - but i dont know how to set - therefor i have try to set the config.files-parameter:
[...]
var files = new VirtualMachineFileInfo();
files.VmPathName = $"[{VM_Datastore}] {newVmName}/{newVmName}.vmx";
//files.VmPathName = $"[{VM_Datastore}]"; --> this wont be work too
config.Files = files;
folder.CreateVM(config, pool, null);
[...]
This correction wont work too. The new exception is, that "The object has already been deleted or has not been completely created"
Normaly (in the script and in the vSphere Web Client) vSphere decides automatically the location of the files.
Is the solution of my problem to set the datastore-parameter? And when it is so - how can i specify this? Or I have to set the files-Property - but how?
Or I have to do it completely differently?
PS:
I have check the class VMCreate.cs of the Vmware-Samples2008. But this is much more complex then the simple script. There must be a simple solution or not?