PowerCLI

 View Only
  • 1.  Re-generate MAC addresses for VMs

    Posted Jan 06, 2015 07:37 AM

    Hi all,

    I'm trying to come up with an easy way of achieving the following:

    My team has stood up a vCenter server and created a number of virtual machines on it.  We have since found out the vCenter ID used on this server is the same as an existing one, thus possibly creating MAC address conflicts.  We have changed the ID on the vCenter server and new virtual machines are created with MAC addresses in a new range.  I'm trying to re-generate the MAC addresses for the existing VMs (about 200).  Using PowerCLI, I'm able to manually set a MAC address, but when I switch it back to assigned, it does not generate a new MAC (the manually set one remains).  The code I'm using is below:

    foreach ($vm in $vmlist)

    {

    # set manual MAC

    $adapter = Get-NetworkAdapter -vm $vm

    $dapter.ExtensionData.AddressType = "Manual"

    $adapter.ExtensionData.MacAddress = "00:50:56:1a:ff:ff"

    Set-NetworkAdapter $adapter -Confirm:$false

    # Set Autogenerated MAC

    $adapter = Get-NetworkAdapter -vm $vm

    $adapter.ExtensionData.AddressType = "Assigned"

    Set-NetworkAdapter $adapter -Confirm:$false

    }


    The vCenter server is running 5.5 and PowerCLI verison is 5.5 Release 1.  At the moment, I'm only testing the code against one virtual machine which is powered off.  Thanks.



  • 2.  RE: Re-generate MAC addresses for VMs

    Posted Jan 06, 2015 08:48 AM

    You can do this by stopping the VM, removing some lines from the VMX file for the VM and then restarting the VM.

    See How to force VMware to generate a new MAC address for a virtual machine

    To remove lines from a VMX file with PowerCLI have a look at  1.  Re: Need to remove advanced values from VMX file



  • 3.  RE: Re-generate MAC addresses for VMs

    Posted Jan 06, 2015 09:10 AM

    The following PowerCLI scripts uses your method to set the MAC address to 00:50:56:1a:ff:ff and manual, and afterwards removes the MAC address and sets it to generated for all of the virtual machines in your environment. The script assumes that all of your virtual machine have only one network adapter.

    foreach ($VM in (Get-VM))
    {
     
    $NetworkAdapter = $VM | Get-NetworkAdapter
     
    $NetworkAdapter | Set-NetworkAdapter -MacAddress 00:50:56:1a:ff:ff -Confirm:$false
     
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
     
    $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
     
    $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
     
    $spec.deviceChange[0].operation = "edit"
     
    $spec.deviceChange[0].device = $NetworkAdapter.ExtensionData
     
    $spec.deviceChange[0].device.addressType = "generated"
     
    $spec.deviceChange[0].device.macAddress = $null
     
    $VM.ExtensionData.ReconfigVM_Task($spec)
    }