Hyper-V v3, PowerShell, Scripting, Virtualization
comments 15

Set (or inject) guest network configuration from Hyper-V host (Windows Server 2012)

Those of you who work(ed) on VMware ESX and use PowerCLI for ESX management would have – at some point in time – used the Set-VMGuestNetworkInterface cmdlet to set the IP address configuration of guest OS from the vCenter host or where you have PowerCLI installed. This is quite useful when performing automated guest OS installs and you want to be able to accurately set IP information for different network interfaces in the guest.

In Hyper-V in Server 2008 R2, there were no interfaces to achieve something like this. However, as I’d mentioned in an earlier post, the updated WMI namespace in Hyper-V in Server 2012 provides a way to set the guest IP information from Hyper-V host.

In a previous post, I showed you how to retrieve Hyper-V guest network configuration information using the updated WMI classes. In this post, we shall see how we can set the guest network information. We will use the new WMI class in Server 2012 Hyper-V WMI namespace – Msvm_GuestNetworkAdapterConfiguration.

Make a note that this works only when you have Hyper-V (Server 2012) integration components running in the guest.

In the above function, I have added only IPv4 support although the WMI class supports IPv6 configuration. The above function can be used to set static or DHCP IP configuration.

The way we use this is simple. We can retrieve the network adapter object for a VM and then use pass it on to the Set-VMNetworkConfiguration function.

The above command will set IP configuration to DHCP on the first network interface in the guest OS.

Make a note here that the network interface enumeration in the guest may not be same as the way it was assigned to the VM. So, if you have assigned different names to the network adapters when assigning them to the virtual machine, you can use the name of the adapter to set the configuration. Also, note that if you are switching the guest configuration from static to DHCP using this function, there seems to be a bug in the guest OS network configuration refresh. The IP configuration changes to DHCP but the network adapter does not release the static IP information. You need to restart guest OS for the changes to come into effect.

You can also set static IP configuration:

The -DefaultGateway and -DNSServer parameters are optional.

There is certainly some more scope to improve this function and I will do that in the coming days.

Filed under: Hyper-V v3, PowerShell, Scripting, Virtualization


Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.