VMware Cloud Community
cldbvrg
Contributor
Contributor
Jump to solution

Set MAC address on vm deployment with new-vm

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.

0 Kudos
1 Solution

Accepted Solutions
CRad14
Hot Shot
Hot Shot
Jump to solution

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?

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂

View solution in original post

0 Kudos
17 Replies
LucD
Leadership
Leadership
Jump to solution

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

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.

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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!"
    }

0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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.

0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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!

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

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.

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
cldbvrg
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Under DeviceInfo, see the VirtualDevice object.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

CRad14
Hot Shot
Hot Shot
Jump to solution

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

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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 ***>

0 Kudos
CRad14
Hot Shot
Hot Shot
Jump to solution

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?

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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"}))

0 Kudos
cldbvrg
Contributor
Contributor
Jump to solution

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!

0 Kudos