private static VirtualMachineNetworkInfo[] GetNetworksByAdapters(Guid vmId, MinimizedVMNetworkAdapter[] networkAdapters) { var scope = new ManagementScope(@"\\.\root\virtualization\v2"); var resultList = new List <VirtualMachineNetworkInfo>(); foreach (var networkAdapter in networkAdapters) { var guestNetworkId = networkAdapter.Id.Replace("Microsoft:", "Microsoft:GuestNetwork\\").Replace("\\", "\\\\"); var obj = new ManagementObject(); var path = new ManagementPath(scope.Path + $":Msvm_GuestNetworkAdapterConfiguration.InstanceID=\"{guestNetworkId}\""); obj.Path = path; obj.Get(); var info = new VirtualMachineNetworkInfo { AdapterName = networkAdapter.Name, IPAddresses = ObjectToStringArray(obj.GetPropertyValue("IPAddresses")), DefaultGateways = ObjectToStringArray(obj.GetPropertyValue("DefaultGateways")), DnsServers = ObjectToStringArray(obj.GetPropertyValue("DNSServers")), DhcpEnabled = (bool)obj.GetPropertyValue("DHCPEnabled") }; info.Subnets = AddressesAndSubnetsToSubnets(info.IPAddresses, ObjectToStringArray(obj.GetPropertyValue("Subnets"))).ToArray(); resultList.Add(info); } return(resultList.ToArray()); }
private String getNetworkName() { String networkName = null; VirtualMachineRuntimeInfo vmRuntimeInfo = (VirtualMachineRuntimeInfo)cb.getServiceUtil().GetDynamicProperty( _virtualMachine, "runtime"); ManagedObjectReference envBrowser = (ManagedObjectReference)cb.getServiceUtil().GetDynamicProperty( _virtualMachine, "environmentBrowser"); ManagedObjectReference hmor = vmRuntimeInfo.host; if (hmor != null) { ConfigTarget configTarget = cb.getConnection()._service.QueryConfigTarget(envBrowser, null); if (configTarget.network != null) { for (int i = 0; i < configTarget.network.Length; i++) { VirtualMachineNetworkInfo netInfo = configTarget.network[i]; NetworkSummary netSummary = netInfo.network; if (netSummary.accessible) { if (netSummary.name.Equals( cb.get_option("value"))) { networkName = netSummary.name; break; } } } if (networkName == null) { Console.WriteLine("Specify the Correct Network Name"); return(null); } } Console.WriteLine("network Name " + networkName); return(networkName); } else { Console.WriteLine("No Host is responsible to run this VM"); return(null); } }
public VirtualMachineConfigSpec createVmConfigSpec(String vmName, String datastoreName, int diskSizeMB, ManagedObjectReference computeResMor, ManagedObjectReference hostMor) { ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor); VirtualDevice[] defaultDevices = getDefaultDevices(computeResMor, hostMor); VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); String networkName = null; if (configTarget.network != null) { for (int i = 0; i < configTarget.network.Length; i++) { VirtualMachineNetworkInfo netInfo = configTarget.network[i]; NetworkSummary netSummary = netInfo.network; if (netSummary.accessible) { networkName = netSummary.name; break; } } } ManagedObjectReference datastoreRef = null; if (datastoreName != null) { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.name.Equals(datastoreName)) { flag = true; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; } else { throw new Exception("Specified Datastore is not accessible"); } break; } } if (!flag) { throw new Exception("Specified Datastore is not Found"); } } else { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; flag = true; break; } } if (!flag) { throw new Exception("No Datastore found on host"); } } String datastoreVolume = getVolumeName(datastoreName); VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo(); vmfi.vmPathName = datastoreVolume; configSpec.files = vmfi; // Add a scsi controller int diskCtlrKey = 1; VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec(); scsiCtrlSpec.operation = VirtualDeviceConfigSpecOperation.add; scsiCtrlSpec.operationSpecified = true; VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController(); scsiCtrl.busNumber = 0; scsiCtrlSpec.device = scsiCtrl; scsiCtrl.key = diskCtlrKey; scsiCtrl.sharedBus = VirtualSCSISharing.noSharing; String ctlrType = scsiCtrl.GetType().Name; // Find the IDE controller VirtualDevice ideCtlr = null; for (int di = 0; di < defaultDevices.Length; di++) { if (defaultDevices[di].GetType().Name.Equals("VirtualIDEController")) { ideCtlr = defaultDevices[di]; break; } } // Add a floppy VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec(); floppySpec.operation = VirtualDeviceConfigSpecOperation.add; floppySpec.operationSpecified = true; VirtualFloppy floppy = new VirtualFloppy(); VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo(); flpBacking.deviceName = "/dev/fd0"; floppy.backing = flpBacking; floppy.key = 3; floppySpec.device = floppy; // Add a cdrom based on a physical device VirtualDeviceConfigSpec cdSpec = null; if (ideCtlr != null) { cdSpec = new VirtualDeviceConfigSpec(); cdSpec.operation = VirtualDeviceConfigSpecOperation.add; cdSpec.operationSpecified = true; VirtualCdrom cdrom = new VirtualCdrom(); VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo(); cdDeviceBacking.datastore = datastoreRef; cdDeviceBacking.fileName = datastoreVolume + "testcd.iso"; cdrom.backing = cdDeviceBacking; cdrom.key = 20; cdrom.controllerKey = ideCtlr.key; cdrom.controllerKeySpecified = true; cdrom.unitNumberSpecified = true; cdrom.unitNumber = 0; cdSpec.device = cdrom; } // Create a new disk - file based - for the vm VirtualDeviceConfigSpec diskSpec = null; diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB); // Add a NIC. the network Name must be set as the device name to create the NIC. VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); if (networkName != null) { nicSpec.operation = VirtualDeviceConfigSpecOperation.add; nicSpec.operationSpecified = true; VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.deviceName = networkName; nic.addressType = "generated"; nic.backing = nicBacking; nic.key = 4; nicSpec.device = nic; } var deviceConfigSpec = new List <VirtualDeviceConfigSpec>(); deviceConfigSpec.Add(scsiCtrlSpec); deviceConfigSpec.Add(floppySpec); deviceConfigSpec.Add(diskSpec); deviceConfigSpec.Add(nicSpec); if (ideCtlr != null) { deviceConfigSpec.Add(cdSpec); } configSpec.deviceChange = deviceConfigSpec.ToArray(); return(configSpec); }