Try like this
$vmName = 'MyVM'
Get-VM -Name $vmName | Get-HardDisk |
Select @{N='VM';E={$_.Parent.Name}},
@{N='Harddisk';E={$_.Name}},
@{N='Uuid';E={$_.ExtensionData.Backing.Uuid}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What UUID do you mean?
The one for the datastore on which the VM's files are sitting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The specific UUID of a VM's vmdk (the same code that optain with "vmkfstools -J getuuid")
Try like this
$vmName = 'MyVM'
Get-VM -Name $vmName | Get-HardDisk |
Select @{N='VM';E={$_.Parent.Name}},
@{N='Harddisk';E={$_.Name}},
@{N='Uuid';E={$_.ExtensionData.Backing.Uuid}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many Thanks LucD this is helpfully for me.
Is there a script to change this UUID.
Before write here I have found a lot of blogs or guide that use "Get-View -Id (Get-View ServiceInstance).Content.VirtualDiskManager"
Regards
As long as there are no snapshots, you can do something like this
$vmName = 'MyVM'
$si = Get-View ServiceInstance
$vdkMgr = Get-View -Id $si.Content.VirtualDiskManager
$vm = Get-VM -Name $vmName
$dc = Get-Datacenter -VM $vm
Get-HardDisk -VM $vm | ForEach-Object -Process {
$newGuidArr = [guid]::NewGuid().ToByteArray() | % {$_.tostring("x2")}
$newGuid = "$($newGuidArr[0..7] -join ' ')-$($newGuidArr[8..15] -join ' ')"
Write-Host "Before: $($vdkMgr.QueryVirtualDiskUuid($_.Filename,$dc.ExtensionData.MoRef))"
$vdkMgr.SetVirtualDiskUuid($_.Filename,$dc.Id,$newGuid)
Write-Host "After: $($vdkMgr.QueryVirtualDiskUuid($_.Filename,$dc.ExtensionData.MoRef))"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry LucD,
Maybe I'm not explaining this very well, I will try to explain what I need to script.
I need to implement this check vmdk uuid to exploit the 'seeds' feature on vsphere replication. Say that, on my production site I have all VM information to get vmdk UUID; on my disaster recovery I have only the vmdk (copied or restored) without any other VM's files.
Now I need to compare VM's vmdk UUID with my production and disaster recovery and if they are different, on disaster recovery I'll have to rename it.
Many thanks
Padulka
Let me try to check if I understand that correctly.
On your recovery site you have a datastore that only contains the VMDK.
You now want to check these VMDK if they have the same Uuid as the original VMDK.
Is that correct?
Are the folders/names of the VMDK on the datastore the same as the ones on the original site?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD
Yes it's all correct.
The following script uses the datastore provider to find and access VMDK files.
It then uses the VirtualDiskManager to objtain the VMDK Uuid.
But, while most Uuid seem to correspond with what the previous script in this thread produces, there are some different Uuid in the list.
And to be honest, at this point, I have no explanation why that might happen.
So I'm not sure the script would actually help you in scanning your recovery datastore.
Update: when I run the 2 scripts on datastores on FC LUNs, the Uuids do correspond 100%.
It's only when I use iSCSI based datastores that I notice some discrepancies in some Uuids.
I'm now curious to know if you see corresponding Uuids.
$dsName = 'MyDatastore'
$ds = Get-Datastore -Name $dsName
$si = Get-View ServiceInstance
$vmdkMgr = Get-View -Id $si.Content.VirtualDiskManager
New-PSDrive -Datastore $ds -Name DS -PSProvider vimdatastore -Root '\' | Out-Null
Get-ChildItem -Path DS: -Recurse -Filter '*.vmdk' -Exclude '*-flat.vmdk' |
ForEach-Object -Process {
$vc = $_.FullName.Split('\')[1].Split('@')'[0]
$dummy,$dc,$ds,$path = $_.FullName.Split('@')[1].Split('\',[System.StringSplitOptions]::RemoveEmptyEntries)
$path = $path -join '/'
$uri = "https://$vc/folder/$($path)?dcPath=$dc&dsName=$ds"
$hexUuid = $vmdkMgr.QueryVirtualDiskUuid($uri,$null)
$hexUuid = $hexUuid.Split(' -')
[pscustomobject]@{
File = $_.DatastoreFullPath
Uuid = "{0:x8}-{1:x4}-{2:x4}-{3:x4}-{4:x12}" -f ($hexUuid[0..3] -join ''),
($hexUuid[4..5] -join ''),
($hexUuid[6..7] -join ''),
($hexUuid[8..9] -join ''),
($hexUuid[10..15] -join '')
}
} | Sort-Object -Property File
Remove-PSDrive -Name DS -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I'm testing in my lab.
I'm trying to create a variable to avoid to write manually datastore, as your script:
$dsName = 'MyDatastore'
with this:
$dsName = Get-VM -Name $vmName | Get-HardDisk | Select @{N="Name";E={$_.FileName.Split("]")[0].TrimStart("[")}} | Select -Last 1
but the second variable on your script ($ds = Get-Datastore -Name $dsName) return with errore "Get-Datastore Datastore with name '@{Name=DatastoreName}' was not found using the specified filter(s)."
Try like this
$dsName = Get-VM -Name $vmName | Get-HardDisk | %{$_.FileName.Split("]")[0].TrimStart("[")} | Select -First 1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
follow the final script that is helpfull for my question, many thanks. (sorry I don't know how to put color as your replies)
if ($vrvmname01 -eq "")
{
$vrvmsinobj = get-folder -name "foldersvms" | get-vm
Write-Host "CHECK UUID ALL VMs" -ForegroundColor Black -BackgroundColor Yellow
foreach ($vrvminobj in $vrvmsinobj)
{
""
""
Get-VM -Name $vrvminobj | Get-HardDisk |
Select @{N='VM';E={$_.Parent.Name}},
@{N='Harddisk';E={$_.Name}},
@{N='Uuid';E={$_.ExtensionData.Backing.Uuid}},
@{N='Path';E={$_.FileName.Split("/")[1].TrimStart("")}},
@{N='Capacity';E={$_.CapacityGB}} |
Format-Table -AutoSize
}
}
else {
""
"________________________________________________________"
""
Write-Host "Verifica UUID $vrvmname01" -ForegroundColor Black -BackgroundColor Yellow
"________________________________________________________"
$vrvmname01state = Get-VM -Name $vrvmname01 | Select PowerState -ExpandProperty PowerState
if ($vrvmname01state -eq "PoweredOn")
{
"________________________________________________________"
""
Write-Host "$vrvmname01 è ACCESA" -ForegroundColor Yellow
"________________________________________________________"
"________________________________________________________________________________________________"
""
}
Get-VM -Name $vrvmname01 | Get-HardDisk |
Select @{N='VM';E={$_.Parent.Name}},
@{N='Harddisk';E={$_.Name}},
@{N='Uuid';E={$_.ExtensionData.Backing.Uuid}},
@{N='Path';E={$_.FileName.Split("/")[1].TrimStart("")}},
@{N='Capacity';E={$_.CapacityGB}} |
Format-Table -AutoSize
"________________________________________________________________________________________________"
}
$vrdsname01 = Get-VM -Name $vrvmname01 | Get-HardDisk | %{$_.FileName.Split("]")[0].TrimStart("[")} | Select -First 1
$vrds01 = Get-Datastore -Name $vrdsname01
$vrsi = Get-View ServiceInstance
$vrvmdkmgr = Get-View -Id $vrsi.Content.VirtualDiskManager
$vrexfile = @('*-flat.vmdk','*-ctk.vmdk')
New-PSDrive -Datastore $vrds01 -Name DS -PSProvider vimdatastore -Root '\' | Out-Null
""
""
"________________________________________________________"
""
Write-Host "Verifica UUID tutti i vmdk con nome: "$vrvmname01"" -ForegroundColor Black -BackgroundColor Yellow
"________________________________________________________"
"______________________________________________________________________________________________________________________"
Get-ChildItem -Path DS: -Recurse -Filter "$vrvmname01*.vmdk" -Exclude $vrexfile |
ForEach-Object -Process {
$vc = $_.FullName.Split('\')[1].Split('@')[0]
$dummy,$dc,$vrds01,$path = $_.FullName.Split('@')[1].Split('\',[System.StringSplitOptions]::RemoveEmptyEntries)
$path = $path -join '/'
$uri = "https://$vc/folder/$($path)?dcPath=$dc&dsName=$vrds01"
$hexUuid = $vrvmdkmgr.QueryVirtualDiskUuid($uri,$null)
$hexUuid = $hexUuid.Split(' -')
[pscustomobject]@{
File = $_.DatastoreFullPath
Uuid = "{0:x8}-{1:x4}-{2:x4}-{3:x4}-{4:x12}" -f ($hexUuid[0..3] -join ''),
($hexUuid[4..5] -join ''),
($hexUuid[6..7] -join ''),
($hexUuid[8..9] -join ''),
($hexUuid[10..15] -join '')
}
} | Sort-Object -Property File
"______________________________________________________________________________________________________________________"
Remove-PSDrive -Name DS -Confirm:$false