Create a Virtual Machine with HyperV WMI Provider v2 in C# RRS feed

  • Question

  • 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://docs.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;
                CimInstance virtualSystemSettingData = new CimInstance("Msvm_VirtualSystemSettingData");
                CimProperty cimPropertyElementname = CimProperty.Create("ElementName", serverName, CimType.String, CimFlags.ReadOnly);
                //virtualSystemSettingData.CimInstanceProperties["ElementName"].Value = serverName;
                // Generation 2 
                CimProperty cimPropertyVirtualSystemSubType = CimProperty.Create("VirtualSystemSubType", "Microsoft:Hyper-V:SubType:2", CimType.String, CimFlags.ReadOnly);
                //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);
                //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);

    Wednesday, September 30, 2020 9:05 AM


All replies

  • Hi Rudinator,

    Thank you for posting here.

    In order to help you find the most suitable forum, I have moved the thread to 'where is the forum for' forum.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 1, 2020 6:13 AM
  • Thursday, October 1, 2020 6:36 AM