I have a issue with duplicate UUIDs and was looking for a script to run on all the duplicates. I found a blog post with a solution but am unable to get it to work.
https://www.derekseaman.com/2010/10/making-your-vmware-vm-uuids-unique.html
The script works fine but errors out at $vm.Extensiondata.ReconfigVM_Task($spec) I can only figure this is because it is an old post and I am on 6.0.
Can someone help me get this to work?
Here is the error:
VM: EQRTEST03
New UUID: 4221873a-7f85-c9f3-ead5-214626065110
You cannot call a method on a null-valued expression.
At D:\IT Tools\VMWARE\vSphereScripts\Change UUID\CHANGE_UUID.ps1:13 char:1
+ $VM.Extensiondata.ReconfigVM_Task($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Here is the script:
# Usage: .changeUUID.ps1
if ($args[0].length -gt 0) {
connect-viserver $args[0]
$VMs = get-vm
foreach ($vm in $VMs){
$date = get-date -format “dd hh mm ss”
$newUuid = “56 4d 50 2e 9e df e5 e4-a7 f4 21 3b ” + $date
echo “VM: ” $VM.name “New UUID: ” $newuuid
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.uuid = $newUuid
$vm.Extensiondata.ReconfigVM_Task($spec)
start-sleep -s 2
}
}
Try changing that first line into
$VMs = Get-VM -Name (Get-content 'D:\VMlist.csv')
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That script seems to be running ok in vSphere 6 for me.
I suspect there might be an issue with that specific VM.
Does
Get-VM -Name EQRTEST03
return a valid VirtualMachine object?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD thanks for the response,
I have been playing with this and I did the steps manually by typing each command. It appears that my for each statement in the script is not passing the variable $VM for the Extensiondata.
PowerCLI C:\> $VM = Get-VM EQRTEST03
PowerCLI C:\> $VM
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
EQRTEST03 PoweredOn 2 4.000
PowerCLI C:\> $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
PowerCLI C:\> $spec.uuid = '56 4d 50 2e 9e df e5 e4-a7 f4 21 3b aa 11 bb 22'
PowerCLI C:\> $vm.Extensiondata.ReconfigVM_Task($spec)
Type Value
---- -----
Task task-731
Any ideas?
I should also mention I modified the script to use Get-content and I only have the EQRTEST01 VM in the csv. But if it works in my echo statement why does it not work in the next line?
$VMs = Get-content 'D:\VMlist.csv'
foreach ($VM in $VMs){
$date = get-date -format “dd hh mm ss”
$newUuid = “56 4d 50 2e 9e df e5 e4-a7 f4 21 3b” + $date
echo “VM: ” $VM “New UUID: ” $newuuid
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.uuid = $newUuid
echo “VM: ” $VM
$VM.Extensiondata.ReconfigVM_Task($spec)
start-sleep -s 2
Try changing that first line into
$VMs = Get-VM -Name (Get-content 'D:\VMlist.csv')
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That was it, can you explain what the () do?
They set the evaluation order, first the expression between () will be executed.
So this Import-Csv will read all the VMnames from the CSV.
The result will be an array of strings.
This array of strings is passed on the Name parameter of the Get-VM cmdlet.
And that returns all the VirtualMachine objects corresponding with the names in the CSV
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference