5

I have a scenario that involves building out a ton of virtual machines in Hyper-V, I cannot use SCVMM and must use a scripted method to create these VMs. I have a CSV with all required information filled out so my Create-VM code works and creates the VMs with no problem.

My issue is, I would like to assign a static MAC address to the VM NICs during creation. This will help me setup DHCP IP reservations as per my lab requirements. Once I create the VM, I do this command:

Set-VMNetworkAdapterVlan -VMName $VMName -Access -ComputerName $VMHost -VlanId $VLANID | Get-VMNetworkAdapter | Set-VMNetworkAdapter -StaticMacAddress $Config.intMAC

At this time I have a NIC already added to this VM and in theory it should set the MAC address to whatever $Config.intMAC holds but it does not, the radio box is still selected for dynamic MAC address and the static value remains unselected and all 0's.

How would I assign the MAC pro-grammatically?

nGX
  • 344
  • 1
  • 6
  • 19

1 Answers1

5

try:

get-vm -name $vmname | Get-VMNetworkAdapter | Set-VMNetworkAdapter -StaticMacAddress "00112233445566"

remember that if you use a variable that the mac address is a string.

Jim B
  • 24,081
  • 4
  • 36
  • 60
  • In general this solution works and I had already tried this method but it kept erroring out, after some trial/error I figured out the MAC must not have any special characters (: or -) otherwise it will not assign the static address. Thank you for the quick help – nGX Sep 04 '13 at 02:21