Change number of processors on an existing VM
Closed this issue · 3 comments
Hi,
I am so happy that I found this project. I have been banging my head the last week.
I have already imported a VM, and now I want to change the number of processors of the virtual machine. I can only find this in the CreateVirtualMachine (in VMMS_public.cs).
My question is: Is there a way to change the number of processors on an existing VM?
(I see that this is relates to another "issue" here (#7), where they want to change something on an existing VM).
Comment: The reason for changing in an existing VM is, that I use TPM, and the TPM is imported and checked against the Host Guardian Service. So it will not work for me to make a new VM, unless I can import only the TPM (I don't thisnk that is possible).
I think I just found a solution (I used the code from https://github.com/jscarle/HyperV.NET):
// Main program:
ManagementObject systemSettings = CreateSettings(Settings.System);
ManagementObject virtualMachine = GetVm.GetVmClass.GetVm(".", "MyVm");
ManagementObject processorSettings = GetRelatedSettings(systemSettings, Settings.Processor);
processorResource["VirtualQuantity"] = 4;
ModifyResourceSettings(new ManagementObject[] { processorSettings }, out _);
I used these functions from https://github.com/jscarle/HyperV.NET. But may have changed them a bit. Remember to include the WMI_extensions file form the repo (for ToStringArray() and more):
// Extra functions and stuff:
static internal ManagementObject CreateSettings(Settings settings)
{
string hostname = ".";
System.Management.ManagementScope virtualizationScope = new System.Management.ManagementScope(@"\\" + hostname + @"\root\virtualization\v2", null);
using (ManagementClass managementClass = new ManagementClass(SettingsClass(settings)))
{
managementClass.Scope = virtualizationScope;
return managementClass.CreateInstance();
}
}
static internal void ModifyResourceSettings(ManagementObject[] resourceSettings, out ManagementObject[] resultingResourceSettings)
{
string hostname = ".";
System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" + hostname + @"\root\virtualization\v2", null);
using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope))
using (ManagementBaseObject inputParameters = managementService.GetMethodParameters("ModifyResourceSettings"))
{
inputParameters["ResourceSettings"] = resourceSettings.ToStringArray();
using (ManagementBaseObject outputParameters = managementService.InvokeMethod("ModifyResourceSettings", inputParameters, null))
{
WmiUtilities.ValidateOutput(outputParameters, scope);
resultingResourceSettings = ((string[])outputParameters["ResultingResourceSettings"]).ToObjectArray();
}
}
}
internal static ManagementObject GetRelatedSettings(ManagementObject instance, Settings settings)
{
return WmiUtilities.GetFirstObjectFromCollection(instance.GetRelated(SettingsClass(settings)));
}
Hello, please help me, thank you very much. I want to implement a custom system based on the image in the vhdx template, get the specified system installation based on the image name, and customize the password
你好,
我很高兴找到这个项目。上周我一直在敲头。
我已经导入了一个虚拟机,现在我想更改虚拟机的处理器数量。我只能在 CreateVirtualMachine(在 VMMS_public.cs 中)中找到它。
我的问题是:有没有办法更改现有虚拟机上的处理器数量?
(我发现这与这里的另一个“问题”( #7 )有关,他们想要更改现有虚拟机上的某些内容)。
评论:在现有 VM 中进行更改的原因是,我使用 TPM,并且导入 TPM 并根据 Host Guardian 服务进行检查。因此,除非我只能导入 TPM(我不认为这是可能的),否则我无法创建新的 VM。
我想我刚刚找到了一个解决方案(我使用了https://github.com/jscarle/HyperV.NET中的代码):
// Main program: ManagementObject systemSettings = CreateSettings(Settings.System); ManagementObject virtualMachine = GetVm.GetVmClass.GetVm(".", "MyVm"); ManagementObject processorSettings = GetRelatedSettings(systemSettings, Settings.Processor); processorResource["VirtualQuantity"] = 4; ModifyResourceSettings(new ManagementObject[] { processorSettings }, out _);
我使用了https://github.com/jscarle/HyperV.NET中的这些函数。但可能已经改变了一些。请记住包含存储库中的 WMI_extensions 文件(对于 ToStringArray() 等):
// Extra functions and stuff: static internal ManagementObject CreateSettings(Settings settings) { string hostname = "."; System.Management.ManagementScope virtualizationScope = new System.Management.ManagementScope(@"\\" + hostname + @"\root\virtualization\v2", null); using (ManagementClass managementClass = new ManagementClass(SettingsClass(settings))) { managementClass.Scope = virtualizationScope; return managementClass.CreateInstance(); } } static internal void ModifyResourceSettings(ManagementObject[] resourceSettings, out ManagementObject[] resultingResourceSettings) { string hostname = "."; System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" + hostname + @"\root\virtualization\v2", null); using (ManagementObject managementService = WmiUtilities.GetVirtualMachineManagementService(scope)) using (ManagementBaseObject inputParameters = managementService.GetMethodParameters("ModifyResourceSettings")) { inputParameters["ResourceSettings"] = resourceSettings.ToStringArray(); using (ManagementBaseObject outputParameters = managementService.InvokeMethod("ModifyResourceSettings", inputParameters, null)) { WmiUtilities.ValidateOutput(outputParameters, scope); resultingResourceSettings = ((string[])outputParameters["ResultingResourceSettings"]).ToObjectArray(); } } } internal static ManagementObject GetRelatedSettings(ManagementObject instance, Settings settings) { return WmiUtilities.GetFirstObjectFromCollection(instance.GetRelated(SettingsClass(settings))); }
Hello, please help me, thank you very much. I want to implement a custom system based on the image in the vhdx template, get the specified system installation based on the image name, and customize the password
I no longer have access to an environment in which I can test this.