Hello..
I was wondering if anyone had a script that would allow me to output a list of VMs, their provisioned space (as a #, with no gb after it), and their used space (as a #, no gb after it). Id then like to take this data into excel, add it all up and see how much disk space we've saved on our SAN by using thin provisioning.
Does anyone have any idea? I think a script to do something like this could be quite useful for a lot of folks who can go back to management and show how much $ they've saved in SAN disk by using this "new" feature.
Thanks!
The only way I could get the "Divide by zero" error was when I made one or more of the datastores unavailable.
The following script excludes the unavailable datatsores
$report = @() $vms = Get-View -ViewType VirtualMachine foreach($vm in $vms){ foreach($perDS in $vm.Storage.PerDatastoreUsage){ if($perDS -ne $null){ $row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc $row.VMname = $vm.Name $row.DSname = (Get-View -Id $perDS.Datastore).Name $row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB) $row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB) $row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100) $report += $row } } } $report | ft
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Just wrote this after reading this post: , it's in Perl but I'm sure one of the PowerCLI gurus will be able to provide you with the snippet of PS code.
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".
I would start here, with some modifications it might be what you are after:
FindThinDisks.ps1
Identifies VMs and templates that are using thin-provisioned virtual disks.
Version 1.0 January 14, 2009
Eric Gray
$vmtp = Get-VM
$vmtp += Get-Template
foreach($vm in $vmtp | Get-View){
foreach($dev in $vm.Config.Hardware.Device){
if(($dev.GetType()).Name -eq "VirtualDisk"){
if($dev.Backing.ThinProvisioned -eq $true) {
$vm.Name + "`t" + $dev.Backing.FileName
}
}
}
}
Hope this helps I got it from here.
Dan
As of vSphere, it's easier than that. If you look at each VM, whether or not it's thin provisioned, you'll get provisioned space and used space. This data is provided at each VM and aggregates the total amount that is committed and uncommitted for each datastore a VMDK from a given VM is stored in. You just need to extract this information and output it in the format you would like
http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.StorageInfo.html
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".
Thanks.. I'll try to figure something out using that. My PS knowledge is
limited, so if anyone feels like throwing together a snippet of code,
please feel free.
lamw
<communities-emai
ler@vmware.com> To
Steve <sdion@apc.com>
09/27/2009 11:49 cc
AM
Subject
message: "Script to determine disk
space saved by thin provisioning"
Steve,
A new message was posted in the thread "Script to determine disk space
saved by thin provisioning":
http://communities.vmware.com/message/1374998#1374998
Author : lamw
Profile : http://communities.vmware.com/people/lamw
Message:
Freely borrowed from Wiliam's Perl script, my attempt
$report = @() $vms = Get-View -ViewType VirtualMachine foreach($vm in $vms){ foreach($perDS in $vm.Storage.PerDatastoreUsage){ $row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc $row.VMname = $vm.Name $row.DSname = (Get-View -Id $perDS.Datastore).Name $row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB) $row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB) $row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100) $report += $row } } $report | ft
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I seem to be getting a lot of errors when running that "$row.DSname =
(Get-View -Id <<<< $perDS.Datstore).Name Attempted to dvide by 0"
LucD
<communities-emai
ler@vmware.com> To
Steve <sdion@apc.com>
09/28/2009 05:13 cc
PM
Subject
message: "Script to determine disk
space saved by thin provisioning"
Steve,
A new message was posted in the thread "Script to determine disk space
saved by thin provisioning":
http://communities.vmware.com/message/1376134#1376134
Author : LucD
Profile : http://communities.vmware.com/people/LucD
Message:
Thanks for looking at this BTW, I appreciate your effort.
LucD
<communities-emai
ler@vmware.com> To
Steve <sdion@apc.com>
09/28/2009 05:13 cc
PM
Subject
message: "Script to determine disk
space saved by thin provisioning"
Steve,
A new message was posted in the thread "Script to determine disk space
saved by thin provisioning":
http://communities.vmware.com/message/1376134#1376134
Author : LucD
Profile : http://communities.vmware.com/people/LucD
Message:
Here is a snippet I have been using. it does NOT use the vSphere specific SDKs, so if there are thin-provisioned templates in a VI3 environment, it can be slightly modified to include those as well. I basically take the standard VMware.Vim.VirtualDevice object type and extend its properties.
Get-VM | Get-View | ForEach-Object { $vmname = $_.Name $uncommittedKB = ($_.Summary.Storage.Uncommitted/1024) $_.Config.Hardware.Device | Where {($_.GetType()).Name -eq "VirtualDisk"} | ForEach-Object { if($_.Backing.ThinProvisioned -eq $true){ $_.PSObject.TypeNames.Insert(0,"$($_.PSObject.TypeNames[0])_Thin#VmwarePowerPackExtension") $_ ` | Add-Member -MemberType NoteProperty -Name ThinProvisioned -Value $_.Backing.ThinProvisioned -PassThru ` | Add-Member -MemberType NoteProperty -Name FileName -Value $_.Backing.FileName -PassThru ` | Add-Member -MemberType NoteProperty -Name DeviceName -Value $_.DeviceInfo.Label -PassThru ` | Add-Member -MemberType NoteProperty -Name VMName -Value $vmname -PassThru ` | Add-Member -MemberType NoteProperty -Name UncommittedKB -value $uncommittedKB -PassThru } } }
That should be $perDS.Datastore.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I must be missing something.... I do see $perDS.Datastore in your script...
$row.DSname = (Get-View -Id $perDS.Datastore).Name
Hi LucD, could you explain what you meant? Thanks in advance.
I think you might have a typo in the script.
Your error message says
$row.DSname =(Get-View -Id <<<< $perDS.Datstore).Name
That should be
$row.DSname = (Get-View -Id $perDS.Datastore).Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah, sorry.. that only appears in the output of the error, it is not contained in the script. Any other ideas?
The only way I could get the "Divide by zero" error was when I made one or more of the datastores unavailable.
The following script excludes the unavailable datatsores
$report = @() $vms = Get-View -ViewType VirtualMachine foreach($vm in $vms){ foreach($perDS in $vm.Storage.PerDatastoreUsage){ if($perDS -ne $null){ $row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc $row.VMname = $vm.Name $row.DSname = (Get-View -Id $perDS.Datastore).Name $row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB) $row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB) $row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100) $report += $row } } } $report | ft
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked perfectly.. thank you!!!
Two last questions..
1. Is there an easy way to export this to CSV? (so I can put the data in excel, total things up, do whatever else).
2. Can this be applied to paritcular clusters only?
Sure.
1) You can pipe the Get-Cluster and Get-Vm cmdlets together to get only the VMs on a specific cluster
2) Pipe the report to an Export-Csv cmdlet
$report = @() $vms = Get-Cluster <your-cluster-name> | Get-Vm | Get-View foreach($vm in $vms){ foreach($perDS in $vm.Storage.PerDatastoreUsage){ if($perDS -ne $null){ $row = "" | Select VMname, DSname, ThinProvisioned, ThinUsed, SavedPerc $row.VMname = $vm.Name $row.DSname = (Get-View -Id $perDS.Datastore).Name $row.ThinProvisioned = "{0:f2}" -f (($perDS.Committed + $perDS.Uncommitted) / 1GB) $row.ThinUsed = "{0:f2}" -f ($perDS.Committed / 1GB) $row.SavedPerc = "{0:f0}" -f ($perDS.Uncommitted / ($perDS.Committed + $perDS.Uncommitted) * 100) $report += $row } } } $report | Export-Csv "C:\VM-DS-Usage.csv" -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
Thanks for script .
I have run the script and its getting Thick disk also in report.
How to exclude Thick disk from script ?