Creating Virtual Server with HyperV WMI Provider in C#

pickle rick 1 Reputation point
2020-10-06T07:37:12.887+00:00

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);  
        }  
Hyper-V
Hyper-V
A Windows technology providing a hypervisor-based virtualization solution enabling customers to consolidate workloads onto a single server.
2,730 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. TimCerling(ret) 1,156 Reputation points
    2020-10-06T16:16:21.67+00:00

    Is there a reason why you are trying to accomplish your goal in C#? There are many scripts in PowerShell and other languages that already do this.

    This forum is geared more toward operating system and its management. Your question is about programming. You may have better results by posting to a C# programming forum in MSDN. (I could not find a C# programming tag for these docs forums.)

    2 people found this answer helpful.

  2. Eric Siron 1,566 Reputation points MVP
    2020-10-08T19:48:03.68+00:00

    You can't change very much during creation. You have to instruct the management service to build the object first. Once Hyper-V builds an Msvm_ComputerSystem object, it will have a lot of associated second- and third-tier CIM objects that you can modify. All storage types are distinct objects from the virtual machine and must be created in a second step and attached in a third.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.