none
Powershell script to assign IP-address to a VM during deployment RRS feed

  • General discussion

  • Hi All,

    Does anybody know of a way to leverage powershell to assign a static ip-addr to a 2012 R2 Hyper-V virtual machine ( Windows for instance) when creating the VM thru powershell ?

    We are looking to automate the process of creating a VM via a powershell script. My script uses the New-VM command to create the VM itself and then power it ON.  But how do i assign it an ip once its powered on to connect to it remotely ?

    The "New-NetIPAddress" cmdlet wont work in this case as this being a new VM doesn't have an IP to begin with so we cannot connect to it remotely to run this command.  The goal is to avoid using Hyper-V manager to manually assign an ip-addr.

    Thanks

    Neeraj




    • Edited by Neeraj_Shah Wednesday, July 24, 2019 4:07 PM
    • Changed type Bill_Stewart Thursday, September 5, 2019 9:49 PM
    • Moved by Bill_Stewart Thursday, September 5, 2019 9:49 PM This is not "scripts on demand"
    Wednesday, July 24, 2019 3:57 PM

All replies

  • Please read first:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 24, 2019 5:13 PM
  • You have to create an adapter and add it to the VM first.

    For help use VMs post in the HyperV forum or review the tutorials that show how to build VMs.


    \_(ツ)_/

    Wednesday, July 24, 2019 8:56 PM
  • Wednesday, July 24, 2019 8:58 PM
  • Here is a good place to start learning how to create VMs in code: https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/create-a-virtual-machine-in-Hyper-V


    \_(ツ)_/

      Hi Jrv, thanks but as i mentioned i am already using those cmdlets such as New-VM as per the link you mentioned. My question is not about how to create a VM. That part is already done. It is about how further can I assign an ip-addr to the VM for which there is no cmdlet available even after adding the adapter to the VM.
    Friday, July 26, 2019 3:31 PM
  • Please read first:

    This forum is for scripting questions rather than script requests


    -- Bill Stewart [Bill_Stewart]

      Hi Bill,  That is exactly what i am doing. I have asked a scripting question, asking for inputs on how to go about a particular task . I am not requesting for a ready made script or somebody's code.   As you can see, i have also mentioned what all options within powershell i myself have tried but didn't get the result. If this doesn't qualify for scripting question then i am not sure what does.
    Friday, July 26, 2019 3:34 PM
  • An IP is not assigned to a VM. It is assigned to an adapter. You need to add and configure the adapter.

    Please read all of the linked articles in the link I posted.


    \_(ツ)_/

    Friday, July 26, 2019 3:35 PM
  • Hi Jrv, Thanks for responding but that Set-VMNetworkadapter doesn't have any options / flags to configure an IP-address for the VM.

    That command is useful to attach a nic and assign a static MAC-address.


    Friday, July 26, 2019 5:33 PM
  • AS noted above. Add a switch, net adapter and configure the net adapter.

    Here are the Cmdlets for managing Hyper-V

    https://docs.microsoft.com/en-us/powershell/module/hyper-v/?view=win10-ps


    \_(ツ)_/

    Friday, July 26, 2019 8:34 PM
  • Hey Neeraj,

    Hyper-V by itself can't assign a IP address to a guest VM. There are however other available tools for doing this.

    The most common is to have DHCP available on the network so that the VMs will get a dynamic IP by default, and then you can use remote powershell to change this to a static IP.

    Next is using Unattend.xml files, which can be inserted into the VM's VHD, or attached to the VM using a temp VHD, and then when the VM boots for the first time, it will use the Unattend file to do things like join the domain, set Admin creds, change IP address etc.

    The final suggestion is only available when using Windows Server 2016 or later Hyper-V hosts, and Windows Server 2016 or later Hyper-V Guests in the VMs, and it's to use Powershell Direct. Powershell Direct allows you to run powershell in the VM from the Host without a network connection. From there you can use standard Powershell commands to assign an IP to the VM.

    I hope these help get you started

    Ben


    Cheers, Ben Thomas

    Microsoft Certified Professional
    Microsoft MVP Cloud and Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Friday, July 26, 2019 11:39 PM
  • Hey Neeraj,

    Hyper-V by itself can't assign a IP address to a guest VM. There are however other available tools for doing this.

    What?

    Get-VMNetworkAdapter VM01 NetworkAdapter01 | 
        Set-VMNetworkAdapterFailoverConfiguration -Ipv6Address aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaa:aaaa -Ipv6SubnetPrefixLength 12

    Who invents these crazy ideas?

    I posted the links because no one ever reads the documentation so they get answers from others who have not read the docs.  This leads to huge amounts of bad and wrong information cycling through people brains.

    I do, however, agree that "best practice" is to use DHCP.


    \_(ツ)_/


    • Edited by jrv Friday, July 26, 2019 11:45 PM
    Friday, July 26, 2019 11:45 PM
  • What?

    Get-VMNetworkAdapter VM01 NetworkAdapter01 | 
        Set-VMNetworkAdapterFailoverConfiguration -Ipv6Address aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaa:aaaa -Ipv6SubnetPrefixLength 12

    Who invents these crazy ideas?

    I posted the links because no one ever reads the documentation so they get answers from others who have not read the docs.  This leads to huge amounts of bad and wrong information cycling through people brains.

    I do, however, agree that "best practice" is to use DHCP.


    \_(ツ)_/


    Hey jrv,

    The command you show, will only set the IP address of a Hyper-V Replica VM when it is failed over to the replica copy. It won't set the IP address on a standard VM. It's designed to assist with DR failover.


    Cheers, Ben Thomas

    Microsoft Certified Professional
    Microsoft MVP Cloud and Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Saturday, July 27, 2019 12:05 AM
  • You may be correct.  I always use DHCP so I have not tried it.


    \_(ツ)_/

    Saturday, July 27, 2019 12:10 AM