I have a set of vms in a csv file which I am trying to modify vm advanced settings and unregister and register back as new name in different resource pool but i am failing to remove cloud.uuid and make the network adapter mac as automatic while reconfigvm ($registernewvravm.ExtensionData.ReconfigVM($vmConfigSpec))
$csvimportvms=Import-Csv -Path C:\Users\vk185112\Desktop\vinvappvcloudvms.csv
foreach($vcloudvmscsv in $csvimportvms){
$registernewvravm= Get-VM $vcloudvmscsv.OldVMName
$nicName= Get-NetworkAdapter -VM $registernewvravm
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$vmConfigSpec.extraconfig += New-Object VMware.Vim.optionvalue
$vmConfigSpec.extraconfig[0].Key="cloud.uuid"
$vmConfigSpec.extraconfig[0].Value=$null
$dev.operation = "edit"
$registernewvravm.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $nicName.Name} | %{
$dev.device = $_
if($dev.device.addressType -eq "manual"){
$dev.device.addressType= 'assigned'
$dev.Device.MacAddress = $null
}
}
$vmConfigSpec.DeviceChange += $dev
$registernewvravm.ExtensionData.ReconfigVM($vmConfigSpec)
Remove-VM -VM $registernewvravm.name -DeleteFromDisk:$false -Confirm:$false -RunAsync
$newVM = New-VM -VMFilePath $registernewvravm.ExtensionData.Config.Files.VmPathName -ResourcePool $vcloudvmscsv.NewResourcePool -Name $vcloudvmscsv.NewVMName
Start-VM -vm $newVM
Get-VMQuestion -vm $newVM | Set-VMQuestion -Option "button.uuid.movedTheVM" -Confirm:$false
}
error:
csv file
Does this (setting the MAC to $null), make a difference?
$vm = Get-VM -Name $vmName
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
foreach ($vnic in Get-NetworkAdapter -VM $vm) {
$nic = $vm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $vnic.Name}
$nic.AddressType = 'Generated'
$nic.MacAddress = $null
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec.DeviceChange += $devChange
}
$vm.ExtensionData.ReconfigVM($spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you see the entry "cloud.uuid" when you do a Get-AdvancedSetting for the VM?
Can you try the MAC change on its own?
$nicName = 'Network adapter 1'
$newMAC = "12:34:56:78:00:01"
$vm = Get-VM -Name $vmName
$nic = $vm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $nicName}
$nic.MacAddress = $newMAC
$nic.AddressType = 'Manual'
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $devChange
$vm.ExtensionData.ReconfigVM($spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you see the entry "cloud.uuid" when you do a Get-AdvancedSetting for the VM?
Yes I can see cloud.uuid in advancedsetting
Can you try the MAC change on its own?
Yes by executing this script mac has changed as given in $newMAC but it is still manual. I want mac to be automatic so that vcenter will assign it automatically and map the portgroup given in the csv file.
Then try removing it like this.
Get-AdvancedSetting -Name 'cloud.uuid' |
Remove-AdvancedSetting -Confirm:$false
To set the NIC back to an automatic MAC try like this
$nicName = 'Network adapter 1'
$vm = Get-VM -Name $vmName
$nic = $vm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $nicName}
$nic.AddressType = 'Generated'
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.DeviceChange += $devChange
$vm.ExtensionData.ReconfigVM($spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes this working.
One last question Is it possible to set the NIC back to an automatic MAC if the vm has multiple nic and if there mac is manual.
Yes, provide the correct vNIC via the $nicName variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Like this? or I need to run for every single adapter
$nicName = 'Network adapter 1','Network adapter 2'
Is it possible to run multiple adapters at once?
Try something like this
$vm = Get-VM -Name $vmName
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
foreach ($vnic in Get-NetworkAdapter -VM $vm) {
$nic = $vm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $vnic.Name}
$nic.AddressType = 'Generated'
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec.DeviceChange += $devChange
}
$vm.ExtensionData.ReconfigVM($spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am seeing Mac conflict issues after changing MAC to automatic.
Why this is happening?
Used script:
$csvimportvms=Import-Csv -Path C:\Users\vk185112\Desktop\vinvappvcloudvms.csv
foreach($vcloudvmscsv in $csvimportvms){
$registernewvravm= Get-VM $vcloudvmscsv.OldVMName
#$nicName= Get-NetworkAdapter -VM $registernewvravm
Get-VM -Name $registernewvravm.Name |Get-AdvancedSetting -Name 'cloud.uuid' |Remove-AdvancedSetting -Confirm:$false
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
foreach ($vnic in Get-NetworkAdapter -VM $registernewvravm.Name) {
$nic = $registernewvravm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $vnic.Name}
$nic.AddressType = 'Generated'
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec.DeviceChange += $devChange
}
$registernewvravm.ExtensionData.ReconfigVM($spec)
Remove-VM -VM $registernewvravm.name -DeleteFromDisk:$false -Confirm:$false -RunAsync
$newVM = New-VM -VMFilePath $registernewvravm.ExtensionData.Config.Files.VmPathName -ResourcePool $vcloudvmscsv.NewResourcePool -Name $vcloudvmscsv.NewVMName
Start-VM -vm $newVM
Get-VMQuestion -vm $newVM | Set-VMQuestion -Option "button.uuid.movedTheVM" -Confirm:$false
}
Normally vCenter will fix those and the alarm should disappear.
Does it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have rechecked after sometime but still the Mac address same for the VM's. What might be the issue?
A reboot is also done on the machines
Does this (setting the MAC to $null), make a difference?
$vm = Get-VM -Name $vmName
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
foreach ($vnic in Get-NetworkAdapter -VM $vm) {
$nic = $vm.ExtensionData.Config.Hardware.Device |
where {$_ -is [VMware.Vim.VirtualEthernetCard] -and $_.DeviceInfo.Label -eq $vnic.Name}
$nic.AddressType = 'Generated'
$nic.MacAddress = $null
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$devChange.Device += $nic
$spec.DeviceChange += $devChange
}
$vm.ExtensionData.ReconfigVM($spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes now its working. Thanks
Thanks Lucid. The above code worked perfect to swap from manual to auto mac address type. So i was looking for code which could once changed to Auto mac address type. Get the mac address value from auto type and then set it to manual type with same mac address value generated from auto type
Could you please help on this code
I would strongly advise you not to do that, it might cause duplicate MAC addresses on your network.
Under MAC Address Assignment from vCenter Server under Preventing MAC Address Conflicts, it clearly states
"The MAC address of a powered off virtual machine is not checked against the addresses of running or suspended virtual machines."
So if that VM is powered off, another VM might obtain that same MAC address out of the reserved range.
When the 1st VM is now powered on and since it is not marked as Automatic anymore, the MAC address will not be changed.
Hence a MAC address conflict on the network.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference