Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to configure a Hyper-V virtual machine programmatically, start to finish, using only C# and WMI. Because this is on Windows 10, the WMI namespace is virtualization\V2, which is significantly different from the WMI namespace previous to Windows 8.

At this point I have the virtual machine created, and I have a virtual HD created, but what is lacking is the ability to mount the VHD in the virtual machine.

I have scanned the internet, and found solutions for doing this in the WMI V1 namespace, but samples in V2 is lacking. But from what I can see I need to add a "synthetic disk drive" to the IDE controller as a resource, then add the virtual disk drive onto this. But I get an error from WMI saying it cannot add the resource.

I have seen a lot of examples using PowerShell, but I intend to wrap this in an MSI installer, so that would involve installing PowerShell if necessary. I would prefer to avoid this if at all possible.

What I have tried:

One thing I did do was to take a virtual machine with a VHD mounted, then traversed the resources that were connected from the virtual IDE controller and captured that information. Then I took my "bare" virtual machine and attempted to add the same resources, but again that failed.

I've also been using WMI Explorer 2.0 (by Vinay Pamnani) which I have found to be very helpful in exploring the WMI namespace, but it offers no insight into what I need to do.
Posted
Updated 7-Aug-17 5:25am
Comments
Kornfeld Eliyahu Peter 6-Aug-17 15:05pm    
PowerShell script isn't good?
Andreas Mertens 6-Aug-17 15:57pm    
I am releasing a new product that is hosted in a VM, for clients that are not very sophisticated. I would prefer not installing PowerShell on their machines if I can avoid it.
Kornfeld Eliyahu Peter 6-Aug-17 16:02pm    
PowerShell is included since Windows 7 (v 2.0)...
Andreas Mertens 6-Aug-17 16:10pm    
It is a feature that can be turned on or off, and I would prefer not needing to deal with adding this if I don't have to. And I also don't want to deal with issues in versioning of PowerShell either.

Basically PowerShell is just calling the same WMI objects and methods, so it should be simple to do the same in C#. That is all that I am looking for...

After a lot of sifting through solutions found online and trying them out, I found the following: Attaching a VHD To A VM Using The Hyper-V WMI V2 Namespace – Taylor Brown's Blog[^]

It is written for PowerShell, but it shows exactly how to allocate the resources. The issue I see with the previous solution was that for the second resource (VHD), the path to the VHD was applied to the Connection property, when it should have been applied to the HostResource property.

From what I can tell, the Connection property is for resources that are local to the virtual machine, whereas the HostResource property is for resources that are on the Host system that is to be referenced by the virtual machine.
 
Share this answer
 
Comments
Graeme_Grant 7-Aug-17 10:48am    
5+ ... well done! :)
 
Share this answer
 
Comments
Andreas Mertens 7-Aug-17 10:12am    
I saw that posting as well. Unfortunately it is incorrect and doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900