Hyper-V Getting MAC Address using VM Name
As part of my automated provisioning (yes – System Center and Sysprep will probably be a better option, but hey whatever) script, I need to be able to automatically assign an IP address to a VM. At this point, the only way I can see this being a possibility is to mount the VHD before booting then copying a script that will set the correct IP address to an autostart location.
The ‘script’ is broken down into two parts. The first needs to run on the node, and create the second script inside the VHD. The second script then needs to identify what network adapter/s are present and assign the correct IP address to each.
The first challenge is finding out how to have the second script identify the correct adapter. I initially though about testing connectivity after the IP address was set, and upon a failure, revert the changes and try the next adapter present. Rather than doing this, I found you can get the MAC address for a VM from the Hyper-V node, and filter based on which Virtual Switch the interface is attached to:
The following example will get the MAC address for a VM based on the VM name, and the network it is attached to:
Get-VM -Name NAME | Get-VMNetworkAdapter | Where SwitchName -eq "SWITCHNAME" | Select MacAddress
This returns a MAC address.
I believe I can then use this MAC address in the second part of the script to pull the network adapter name.
Something along the lines of this:
Get-NetAdapter | Where-Object {$_.MacAddress -eq "MACADD"} | Format-List -Property InterfaceDescription
Once I have the correct adapter name, I can use this in an IP address setting script to set the correct IP / gateway etc essentially automating the networking side of provisioning.
At this point there are a few things I have yet to confirm that might make the above attempt futile. First, I do not know if I can run a powershell script on startup – I know there are issues with signed code etc, and this might mean a freshly provisioned system will not allow unsigned code to run – especially if it needs administrator permissions. Second – I don’t know how this will work with a sysprepped image. It is possible (probable?) that with a sysprep image, the network configuration information could be copied to the sysprep config eliminating the need for an autorun script.
You can see an example of a powershell script to assign IP addresses here: https://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/040b8993-d737-4436-8fb1-29187583e7d1/
Hi Chris, Your blog was helpful but I’m still a bit confused. Do we need both the certificate of no…
Hi Daniel, Sorry - I'm not too sure, its possible the certificate of no impediment has changed since I filled…
Hi Aaron, In Japan you should be good to go. You might have difficulty in other countries when trying to…
Hello Chris I have recently married in Japan Konintodoke was filled out and accepted translator advised we were know married…
Hi, Chris. I'm just trying to get my cert of no impediment to marriage done now and I'm a bit…