Hi,
I am running a script that will create a VI property for each VM which will determine the datastore teir based on the name of the datastore where a vmdk resides as follows:
New-VIProperty -ObjectType VirtualMachine –Name TIER2 -Value { param ($vm) return (get-vm $vm | Get-HardDisk | Where-Object {$_.filename -like "*TIER2*"} | Measure-Object -Property capacityGB -sum).sum }
The above is working fine except when a VM that runs on a vSphere 4.1 host has a snapshot, this is because the snapshot file is created in the vmx wogrking directory rather than the datastore where the vmdk resides. These stuffs up the numbers in the report that I create and causes the tier2 total for the snapshott-ed vmdks to appear as tier1 as the snapshot resides in a tier1 datastore (i.e where the vmx is).
Is there a way to run the command above and make it look for the base disk (i.e parent) in the case there is a snapshot? This will resolve the issue as the base disk location will never change.
The following will return the filename of the base VMDK.
From there it shouldn't be too hard to extract the datastorename.
$vmName = "MyVM"
$vm = Get-VM -Name $vmName
foreach($hd in Get-HardDisk -VM $vm){
$vm.ExtensionData.LayoutEx.Disk | where {$_.Key -eq $hd.ExtensionData.Key} | %{
$fileKey = $_.Chain[0].FileKey[0]
$dsFile = $vm.ExtensionData.LayoutEx.File |
where {$_.Key -eq $fileKey}
}
$hd | Select Name,@{N="File";E={$dsFile.Name}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD
That does the job, only thing is that while I have been able to get the sum of all "capacityGB" property, I am unable to create a VI property, it comes back blank, would appreciate it if you could help!
------- here is how i'm getting the total:---------
$vmName = "myvm"
$vm = Get-VM -Name $vmName
foreach($hd in Get-HardDisk -VM $vm){
$vm.ExtensionData.LayoutEx.Disk | where {$_.Key -eq $hd.ExtensionData.Key} | %{
$fileKey = $_.Chain[0].FileKey[0]
$dsFile = $vm.ExtensionData.LayoutEx.File |
where {$_.Key -eq $fileKey}
}
$total += ($hd | Where-Object {$_.filename -like "TIER2*"} | Measure-Object -Property capacityGB -sum).sum
}
$total
--------- when i try to create a property, i do this, but no luck---------
New-VIProperty -ObjectType VirtualMachine -Name TIER2 -Value {
param ($vm) return
foreach($hd in Get-HardDisk -VM $vm){
$vm.ExtensionData.LayoutEx.Disk | where {$_.Key -eq $hd.ExtensionData.Key} | %{
$fileKey = $_.Chain[0].FileKey[0]
$dsFile = $vm.ExtensionData.LayoutEx.File |
where {$_.Key -eq $fileKey}
}
($hd | Where-Object {$_.filename -like "*TIER2*"} | Measure-Object -Property capacityGB -sum).sum
}
$total }
Try like this
New-VIProperty -ObjectType VirtualMachine -Name TIER2 -Value {
param ($vm)
$total = 0
foreach($hd in Get-HardDisk -VM $vm){
$vm.ExtensionData.LayoutEx.Disk | where {$_.Key -eq $hd.ExtensionData.Key} | %{
$fileKey = $_.Chain[0].FileKey[0]
$dsFile = $vm.ExtensionData.LayoutEx.File | where {$_.Key -eq $fileKey}
if($dsFile.Name -match "^\[TIER2"){
$total += $hd.CapacityGB
}
}
}
$total
} -Force | Out-Null
Get-VM TestVM | Select Name,TIER2
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD...
something isnt working right,,, it reports total disks as zero if i run the if statement as follows to report all T2 disks:
if($dsFile.Name -contains "^\[TIER-2") # the key word for our T2 datastores is TIER-2
if i run it like this, it reports the total of all disks that belong to the VM even for the ones stored in a TIER-2 datastore
if($dsFile.Name -notcontains "^\[TIER-2").
any thoughts?
The match that I'm using assumes that the VMDK filename follows this pattern:"[TIER2".
The back-slash is there to escape the special character '['
So if the VMDK filename contains TIER-2, the match (not -contains) should be "[TIER-2".
But this also assumes the datastorename starts with TIER-2, is that correct ?
Or can TIER-2 be anywhere in the datastorename ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference