Automation

 View Only
Expand all | Collapse all

Set MAC address on vm deployment with new-vm

  • 1.  Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 02:29 PM

    I am looking for a way to statically set the network adapter MAC address on deployment. I am using a loop to create 24 copies of a template on multiple VMs and need to set the MAC addresses for DHCP. I was able to have this work using esxi 3.5, but after upgrading to vsphere, I am having issues.

    Is this possible, or do I need to reloop after with set-networkadapter? I am trying to avoid doing this if at all possible.



  • 2.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 04:07 PM

    Afaik you can't do this with the New-VM cmdlet, you'll have to use the Set-NetworkAdapter cmdlet.

    What exactly do you mean with "set the MAC addresses for DHCP" ?

    Perhaps you could include the code that worked for ESXi 3.5 ?



  • 3.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 05:30 PM

    We have DHCP assign addresses based on MAC address of each VM. Our environment gets completely rebuilt every few weeks as a virtual classroom.

    2 -- Here is the part of the function that worked with 3.5, but doesn't appear to be setting the MAC address using vspehere. This is the last part of this process I am having an issue with trying to debug... When I do a write-host $mac, I am getting the correct format, it just isn't being applied to the VMs.

    $tplObj = (Get-View -VIObject $templateName)

    $tplHW = $tplObj.config.hardware.device
        $tplNetDev = $NULL
        foreach ($dev in $tplHW)
        {
            if ($dev -and $dev.deviceInfo.label.Contains("Network Adapter 1"))
            {
                $tplNetDev = $dev
            }
        }
       
        $newVMSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
        # Network Configuration
        if ($tplNetDev)
        {
            $newVMSpec.config = New-Object VMware.Vim.VirtualMachineConfigSpec
            $newVMSpec.config.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
            $newVMSpec.config.deviceChange[0].operation = "edit"
            $newVMSpec.config.deviceChange[0].device = $tplNetDev
            $newVMSpec.config.deviceChange[0].device.addressType = "Manual"
            $newVMSpec.config.deviceChange[0].device.macAddress = $p_mac
            $newVMSpec.config.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardNetworkBackingInfo
            $newVMSpec.config.deviceChange[0].device.backing.deviceName = $classroomNetwork
        }
        else
        {
            Write-Host "Warning: Could not configure network settings!"
        }



  • 4.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 05:31 PM

    So. technically we aren't using New-VM, we are using a function called 'CloneTemplate'. I was just trying to simplify everything so it isn't as many required variables, and is easier to read for anyone else trying to pick it up and build out a virtual classroom.

    If we aren't able to do what I need to with New-VM, I will stick with our current setup, I just need to get it fixed.



  • 5.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 05:40 PM

    Which method do you use in that function ? Is it the CloneVM_Task method ?

    Could you attach that code as well ?

    Are you getting any error messages ?



  • 6.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 05:51 PM

    Precisely.

    $tplObj.CloneVM_Task($destinationFolder.MoRef, $p_name, $newVMSpec)

    No error messages when completing this. I am not turning the VMs on after cloning... Does this change require the VMs to be powered on? I wouldn't think this would change anything, but we were powering on immediately with the old scripts.



  • 7.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 09:09 PM

    No, there shouldn't be a need to power on the VM after the cloning process.

    And are there no errors in the vCenter logs after the cloning process ?

    So the only problem you see after the cloning process is that the MAC address is not set ?



  • 8.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 01:46 PM

    It seems everything about the network part of the deployment script is not working. The VMs are not pointing to the correct network, and the MAC address is not being set. I still cannot get this working correctly.



  • 9.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 03:10 PM

    I think I found where the problem lies.

    $NAME.config.harware.device.... This doesn't allow me to select by label as it used to. .deviceInfo.label.Contains("Network Adapter 1")

    So, what I need to figure out is how to select the Network Adapter when configuring the hardware device.

    $tplObj = "Template Name Goes here"

    $tplHW = $tplObj.config.hardware.device
         $tplNetDev = $NULL
         foreach ($dev in $tplHW)
         {
             if ($dev -and $dev.deviceInfo.label.Contains("Network Adapter 1"))

    If anyone knows how to do this, it would be greatly appreciated!



  • 10.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 03:29 PM

    For you logic line...I would personally do it this way

    if ($dev.deviceinfo.label -like "*Network Adapter 1*")

    It is also good to note that even if there is one network adapter it doesn't mean it will be "Network Adapter 1", if one was added and deleted previously it could be 2, or 3 etc.



  • 11.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 03:51 PM

    Should I be able to use deviceinfo.label now? I have tried multiple variations, but haven't been able to pull anything successfully.

    PowerCLI ** >$tplObj.config.Hardware.Device.deviceinfo.label -like "Network Adapter 1"

    False

    PowerCLI **>$tplObj.config.Hardware.Device.deviceinfo

    The below shows a ton of info, I am only listing the NIC info. Is there a way to select only this? I don't see Label as a valid selection.

    PowerCLI **> $tplObj.config.Hardware.Device

    ***

    ***

    AddressType                : assigned
    MacAddress                 : 00:50:56:ac:21:e1
    WakeOnLanEnabled      : True
    Key                              : 4000
    DeviceInfo                     : VMware.Vim.Description
    Backing                        : VMware.Vim.VirtualEthernetCardNetworkBackingInfo
    Connectable                  : VMware.Vim.VirtualDeviceConnectInfo
    ControllerKey                 : 100
    UnitNumber                   : 7
    DynamicType                 :
    DynamicProperty            :

    PowerCLI **>

    Additionally, the VMs will always come from the same template. I have assured the NIC is "Network Adapter 1", then set back to a template. There shouldn't be any changes to the template that would change the name of the NIC.



  • 12.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 03:54 PM

    Under DeviceInfo, see the VirtualDevice object.



  • 13.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 04:02 PM

    This should show a ton of information to you, are you not seeing anything with this line?

    $tplObj.config.hardware.device.deviceinfo

    If not try this line

    $tplObj.config.hardware.device | Foreach-Object {$_.deviceinfo}

    I think as soon as you see the info you want, it should be cake



  • 14.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 04:14 PM

    Hmmm..

    Did this without any previously set variables. Not sure why the first line isn't working.

    PowerCLI ***> (get-view -VIObject "Template Name").config.hardware.device.DeviceInfo
    PowerCLI ***> (get-view -VIObject "Template Name").config.hardware.device | Foreach-Object{$_.deviceinfo}

    Label                                   Summary                                 DynamicType                             DynamicProperty
    -----                                   -------                                 -----------                             ---------------
    IDE 0                                   IDE 0
    IDE 1                                   IDE 1
    PS2 controller 0                        PS2 controller 0
    PCI controller 0                        PCI controller 0
    SIO controller 0                        SIO controller 0
    Keyboard                                Keyboard
    Pointing device                         Pointing device; Device
    Video card                              Video card
    SCSI controller 0                       BusLogic
    Hard disk 1                             6,144,000 KB
    Network adapter 1                       "Network Name"

    PowerCLI ***> (get-view -VIObject "Template Name").config.hardware.device | Foreach-Object{$_.deviceinfo.label -like "Network adapter 1"}
    False
    False
    False
    False
    False
    False
    False
    False
    False
    False
    True
    PowerCLI ***>



  • 15.  RE: Set MAC address on vm deployment with new-vm
    Best Answer

    Posted Sep 10, 2012 04:18 PM

    So the next step to get just the device object would be

    (get-view -VIObject "Template Name").config.hardware.device | Where-Object {$_.deviceinfo.label -like "Network adapter 1"}

    This is what you are going for correct?



  • 16.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 10, 2012 04:37 PM

    Thank you very much! All works as intended now.  Corrected line below.

         #if ($dev -and $dev.deviceInfo.label.Contains("Network Adapter 1"))
            if ($dev -and ($dev | Where-Object{$_.deviceinfo.label -like "Network adapter 1"}))



  • 17.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 13, 2012 02:35 PM

    FYI:  I had the same problem with a different script cloning out VMs with multiple NICs. The problem was resolved by using the same code, but looping it as well.

    $tplHW = $ttemplateName.config.hardware.device
        $tplNetDev = @()
        foreach ($dev in $tplHW)
        {

    for ($i = 0; $i -lt 4; $i++)
            {
                if ($dev -and ($dev | Where-Object{$_.deviceinfo.label -like "Network adapter $i"}))
                {
                    $tplNetDev += $dev
                }
            }

    Thanks for all the help on this!



  • 18.  RE: Set MAC address on vm deployment with new-vm

    Posted Sep 07, 2012 04:10 PM

    In terms of powercli, I am not sure of another way to do it besides set-networkadapter. There might be a method to do the same thing deep down somewhere in the sdk, but at that point set-networkadapter would be easier anyway.