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

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

by

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.

  • MBasit

    what if i have deployed a linux virtual machine on Hyper-V and want to configure static IP using powershell?

  • A.Agrawal

    I am trying running above script but getting an error where $job.jobstate = 10 (exceptional case). but if I comment out $networksettings.protocolIFType=4096,
    then it runs succesfully , but ip addresses of vm are not changed

  • Pingback: Configure VM Network settings from the host using Hyper-V 2016 – vNiklas Virtualization blog()

  • Stefan Castor

    Is it possible to set the IP from a client over a Host to a VM?

    Client –> Host –> GuestSystem (VM)

    I change the Funktion like:
    $VM = Get-WmiObject -ComputerName “HOSTNAME” -Namespace ‘rootvirtualizationv2’ -Class ‘Msvm_ComputerSystem’ | Where-Object { $_.ElementName -eq $NetworkAdapter.VMName }

    but after that the var $NetworkAdapter doesnt set…

    br

  • Pingback: Create multiple VM’s from CSV and assign IP addresses | ITAbyss()

  • Harish P

    The above command will work for windows server 2008 R2 but we need to do the below changes in the server in order to assign the IP.

    1. .Net frame work 3.5.1 need to be installed on the 2008 r2 Server.
    2. Hyper-v Integration services has to be installed.

    I have tested the same in my environment and its worked like a charm.

    Thanks

    Harish

  • Harish P

    Hi Stefan

    Its possible . but it need some changes in the command .. I will check and share you the updated commands for your query.

  • Stefan Castor

    I already done it, thank You

  • Ramesh Singh

    Hi Harish,

    I’ve Hyper-v 2016, will this solution work in my environment.
    Need you suggestion to achieve it.
    How can I use the above function to achieve it?

    BR,
    Ramesh