How to change Hyper-V VM network settings via WMI

hyper-v

I am not sure where to post this question whether at programmers.stackoverflow.com or here. So please forgive this if i am at the wrong place.

My question is:

  1. How to set Subnet Mask , DNS address and Gateway address of network adapter inside a virtual machine in Hyper-V programmatically via WMI and C#?

I am working on a Hyper-V provisioning system.

Note: I am successfully able to add and retireve IP Address as described here and here

Best Answer

Lots of people have the mistaken impression that a VM is somehow different from a physical machine with respect to networking configuration. There are differences, but they're almost all explained by noting that the VM is connected to a "software switch" which, in the case of Hyper-V, is just like the unmanaged thing you might plug into your office Ethernet tap, allowing you to use several other machines in your office. Think of the VM host machine like your office. Think of the VMs like the machines in your office. That Linksys (or whatever) box in the corner is the VM switch. The tap in the wall is your host's physical port. (Your host probably has more than one physical port. Imagine an office with more than one tap.)

So how do you set the subnet mask and gateway address of the machines in your office? Do you use some office management interface? No, you go to the machines and set them. (Actually, most people use DHCP for this. You might want to do that, too.) Manage the software configuration of the VMs from within the VMs. (Or use some automatic management suite like System Center.)

Now, of course, it doesn't matter if you put in the right gateway address and such if you attach the VM NIC to the wrong network. In my analogy, this amounts to making sure that your machine is plugged into the right tap in your office. This is something that is managed outside the VM.