Hi everybody,
i want to create a virtual hyperV server but I don't know how to set the parameters for virtual system device, random access memory, storage etc.
At the moment the following code can create and set the name of a virtual server. The associated documentation you can find in the Hyper-V v2 Documentation under https://learn.microsoft.com/de-at/windows/win32/hyperv_v2/cim-virtualsystemmanagementservice-definesystem
I think the solution lies in the use of the input parameters but I don't know how to create or use them correctly.
[in] string SystemSettings, [in] string ResourceSettings[]
public async Task CreateVirtualServer(string serverName, string hostName, int storageSize, int ramStorage, int countCores)
{
CimSession session = WMIServiceHelper.GetCimSession(hostName);
IEnumerable<CimInstance> ManagementServiceCollection = session.EnumerateInstances(hvNamespace, "CIM_VirtualSystemManagementService");
CimInstance virtualSystemManagementService = null;
foreach (CimInstance instance in ManagementServiceCollection)
{
string test = instance.CimInstanceProperties["CreationClassName"].ToString();
if (instance.CimInstanceProperties["CreationClassName"].Value.ToString() == "Msvm_VirtualSystemManagementService")
{
virtualSystemManagementService = instance;
break;
}
}
CimInstance virtualSystemSettingData = new CimInstance("Msvm_VirtualSystemSettingData");
CimProperty cimPropertyElementname = CimProperty.Create("ElementName", serverName, CimType.String, CimFlags.ReadOnly);
virtualSystemSettingData.CimInstanceProperties.Add(cimPropertyElementname);
//virtualSystemSettingData.CimInstanceProperties["ElementName"].Value = serverName;
// Generation 2
CimProperty cimPropertyVirtualSystemSubType = CimProperty.Create("VirtualSystemSubType", "Microsoft:Hyper-V:SubType:2", CimType.String, CimFlags.ReadOnly);
virtualSystemSettingData.CimInstanceProperties.Add(cimPropertyVirtualSystemSubType);
//virtualSystemSettingData.CimInstanceProperties["VirtualSystemSubType"].Value = "Microsoft:Hyper-V:SubType:2";
CimInstance randomAccessMemory = new CimInstance("CIM_ResourcePool");
////4 means Memory
CimProperty cimPropertyResourceType = CimProperty.Create("ResourceType", 4, CimType.UInt16, CimFlags.ReadOnly);
randomAccessMemory.CimInstanceProperties.Add(cimPropertyResourceType);
//CimInstance StorageVolume = new CimInstance("Msvm_ResourceAllocationSettingData");
//CimInstance randomAccessMemory = new CimInstance("Msvm_ResourceAllocationSettingData");
CimSerializer serializer = CimSerializer.Create();
//TODO: serialize class here
byte[] serializedClass = serializer.Serialize(virtualSystemSettingData, InstanceSerializationOptions.None);
CimMethodParametersCollection modifySystemSettingsParameters = new CimMethodParametersCollection
{
CimMethodParameter.Create("SystemSettings",System.Text.Encoding.Unicode.GetString(serializedClass),Microsoft.Management.Infrastructure.CimType.String, CimFlags.In)
};
session.InvokeMethod(hvNamespace, virtualSystemManagementService, "DefineSystem", modifySystemSettingsParameters);
}