Quantcast
Channel: VMware Communities : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 178097

Updating MAC address during deployment from template

$
0
0

Hi all,

 

I'm using Shawn Masterson's great script

 

https://blog.smasterson.com/2014/05/21/deploying-multiple-vms-via-powercli-updated-v1-2/

 

to take a CSV list of VMs and their specs and deploy those VMs using powercli

Works great, but I need to customize it for one more property - MAC address.

 

We are creating updated VMs to take over the identities of old VMs (right down to their MAC address)

 

At first I tried the -NetworkAdapterMac <String>, but the VM did not end up with the specified MAC address.

My understanding, this is due to the NetworkAdapterMac parameter meant to specify which MAC you want to operate on, vs actually SETTING the MAC?

 

So this led me to a MAC set script that works (on its own):

 

vNugglets: Setting MAC Address for VM NICs using PowerShell

 

So I attempted to splice the MAC update code into the first script right BEFORE the VM is powered on, passing the Name and Mac:

 

## the name of the VM whose NIC's MAC address to change

$strTargetVMName = $VM.Name

## the MAC address to use

$strNewMACAddr = $VM.Mac

 

## get the .NET view object of the VM

$viewTargetVM = Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device -Filter @{"Name" = "^${strTargetVMName}$"}

## get the NIC device (further operations assume that this VM has only one NIC)

$deviceNIC = $viewTargetVM.Config.Hardware.Device | Where-Object {$_ -is [VMware.Vim.VirtualEthernetCard]}

## set the MAC address to the specified value

$deviceNIC.MacAddress = $strNewMACAddr

## set the MAC address type to manual

$deviceNIC.addressType = "Manual"

 

## create the new VMConfigSpec

$specNewVMConfig = New-Object VMware.Vim.VirtualMachineConfigSpec -Property @{

   ## setup the deviceChange object

   deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec -Property @{

       ## the kind of operation, from the given enumeration

       operation = "edit"

       ## the device to change, with the desired settings

       device = $deviceNIC

   } ## end New-Object

} ## end New-Object

 

## reconfigure the "clone" VM

$viewTargetVM.ReconfigVM($specNewVMConfig)

 

But the above code errors out with:

 

Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration

for device '0'."

At C:\tmp\deployvms12.ps1:355 char:1

+ $viewTargetVM.ReconfigVM($specNewVMConfig)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

 

Does this have to do with the NIC's state?  (not connected?)

 

Feel like I'm close - but if there is an better way to set the MAC before boot, please let me know!

 

Thanks for any info, tips,

 

Fletch


Viewing all articles
Browse latest Browse all 178097

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>