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
Works for me when I replace the first lines with actual values.
Might it be that $VM.Name and $VM.Mac have incorrect values?
I ran this, and it changed the MAC address correctly.
## the name of the VM whose NIC's MAC address to change
$strTargetVMName = 'TestVM'
## the MAC address to use
$strNewMACAddr = '00:50:56:9a:66:e6'
## 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)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Those values are passed from $VM.Name and $VM.Mac from the CSV import
I'll try adding some debug logging in the combined script - because it does indeed work when run standalone.
thanks!
When I logged
Out-Log "Name $vmName will be assigned MAC $vmMac"
I got the whole array of $VM values.
So I assigned
$vmName = $VM.Name
$vmMac = $VM.Mac
AND I update the $headers to include the Mac field I added to the CSV (was not sure if the csvimport did this implicitly)
$headers = "" | Select-Object Name, Boot, OSType, Template, CustSpec, Folder, ResourcePool, CPU, RAM, Disk2, Disk3, Disk4, Datastore, DiskStorageFormat, NetType, Network, DHCP, IPAddress, SubnetMask, Gateway, pDNS, sDNS, Mac, Notes
Now its got the right values, but seems like its failing since the VM nic is not in the "connected" state
Deploying VMs
Name sanssnte01fbc01 will be assigned MAC 00:50:56:ba:3c:7c
Deploying sanssnte01fbc01
All Deployment Tasks Created
Monitoring Task Processing
Reconfiguring sanssnte01fbc01
Setting vCPU(s) and RAM on sanssnte01fbc01
Setting Port Group on sanssnte01fbc01
Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration
for device '0'."
At C:\tmp\deployvms12.ps1:357 char:1
+ $viewTargetVM.ReconfigVM($specNewVMConfig)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Booting sanssnte01fbc01
sanssnte01fbc01 completed with errors
Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for
device '0'."
********************************************************************************
Processing Complete
Any ideas how to have the MAC ReconfigVM call take with the VM's NIC in the correct state?
thanks!
Why do you conclude that the error is related to the connection state of the NIC?
That is not a requirement afaik.
It looks more like there might be another property in the $specNewVMConfig that is incorrect.
You might want to look at the vpxd log, most of the time it contains a clearer indication of the error.
Or perhaps attach the script version that produced that error, so we could have a look
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok, this is the current output with error (I tried setting the checkMACAddress per Powering on a virtual machine after migrating to VMware vCenter Server 5.x fails with error: Invalid...:
(current deployvms12.ps1 attached)
Deploying VMs
Deploying sanssnte01fbc01
All Deployment Tasks Created
Monitoring Task Processing
Reconfiguring sanssnte01fbc01
Setting vCPU(s) and RAM on sanssnte01fbc01
Setting Port Group on sanssnte01fbc01
Name sanssnte01fbc01 will be assigned MAC 00:50:56:4f:4c:4c
Name sanssnte01fbc01 will be assigned MAC 00:50:56:4f:4c:4c
The property 'checkMACAddress' cannot be found on this object. Verify that the
property exists and can be set.
At C:\tmp\deployvms12.ps1:354 char:1
+ $deviceNIC.checkMACAddress = "false"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Booting sanssnte01fbc01
sanssnte01fbc01 completed with errors
7/28/2016 12:42:44 PM Start-VM The operation for the entity "sa
nssnte01fbc01" failed with the following message: "00:50:56:4f:4c:4c is not a va
lid static Ethernet address. It conflicts with VMware reserved MACs for other us
age. ". 00:50:56:4f:4c:4c is not a valid static Ethernet address. It conflicts w
ith VMware reserved MACs for other usage. Invalid MAC address specified. Could n
ot set up "macAddress" for ethernet0. Module DevicePowerOn power on failed. Fail
ed to start the virtual machine. An error was received from the ESX host while p
owering on VM sanssnte01fbc01.
The property 'checkMACAddress' cannot be found on this object. Verify that the p
roperty exists and can be set.
********************************************************************************
******
Processing Complete
The following VMs failed to reconfigure properly:
sanssnte01fbc01
Ok, that message is quite clear, you are trying to use a MAC address that falls into the VMware reserved range for host kernel adapters.
See also Attempt to Power On a Virtual Machine Fails Due to a MAC Address Conflict
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So the you are right - normally this fails - we are trying to set an arbitrary MAC address during deployment:
checkMACAddress = false is the documented way to do this per VMware doc:
This issue occurs when the virtual machine has been configured with a static MAC address in the 00:50:56:xx:xx:xx range.
VMware vCenter Server 5.1 detects this as a protected range and refuses to power on the virtual machine.
This issue is resolved in:
After applying this patch, you can power on the virtual machine by setting the value of ethernetx.checkMACAddress to FALSE in the VMX file of the virtual machine, so that the MAC address is not validated against the range.To change the value for ethernetx.checkMACAddress:
Method 1:
Do you want to automate that scenario?
Or did you already automate it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, we are looking to automate the MAC address assignment during deployment from template (to match the VM we are replacing)
We would like to have the new VM pass the checkMACAddress check at power on - currently it fails.
However, we can run the vNugglets: Setting MAC Address for VM NICs using PowerShell
script to change the powered on VM's MAC address (as seen in vcenter - the ifconfig shows the old MAC)
Crux of the issue is: we can manually make the MAC address any arbitrary value we want in vcenter with the VM off and have it boot fine with that MAC,
Yet, no powercli code has been able to assign the same MAC address in a VM able to pass checkMacAddress checks and boot.
thanks!