Hi All,
Does any one have script to fetch Datastore Folders along with their Folder Size like below.
Datastore Folder Name Folder Size
------------- ----------------- ---------------
Datastore1 VM1 50 GB
Datastore2 VM2 100 GB
Some VMs are registered and Some are not registered.
Need to find those Unregistered VM Folders and need to delete after cross checking it once.
Thanks in Advance.
-Siva
Try something like this
$datastores = Get-Datastore
foreach($ds in $datastores){
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Get-ChildItem -Path DS: | where{$_.ItemType -eq 'Folder' -and $_.Name -notmatch '^\.|^vmk|^esxconsole|tmp'} | %{
New-Object PSObject -Property @{
Datastore = $ds.Name
Folder = $_.Name
SizeGB = [math]::Round((Get-ChildItem -Path "DS:\$($_.Name)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1GB,1)
}
}
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$datastores = Get-Datastore
foreach($ds in $datastores){
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" > $null
Get-ChildItem -Path DS: | where{$_.ItemType -eq 'Folder' -and $_.Name -notmatch '^\.|^vmk|^esxconsole|tmp'} | %{
New-Object PSObject -Property @{
Datastore = $ds.Name
Folder = $_.Name
SizeGB = [math]::Round((Get-ChildItem -Path "DS:\$($_.Name)" | Measure-Object -Property Length -Sum | select -ExpandProperty Sum)/1GB,1)
}
}
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
Thank you so much, your script works like charm.
Could you please let me know the usage of '^' in your script. or any tutorial or example link so that I can go through it.
Thanks & Regards,
Siva
Sure, in the Where clause I wanted to filter out some system folders.
With the -match operator you have a RegEx as the right-hand side operator.
In the RegEx expression you specify one or more 'masks' for which you want a match.
In the RegEx different masks can be separated by an OR (the | symbol).
In each individual mask you have some 'location specifiers', in this case the ^ indicates I want a macth at the begiining of the text.
I.e. '^abc' means match 'abc' at he beginning of the string. So 'abcd' will match, but 'xyzabc' will not.
There are some free learning resources available.
A good one is the Master PowerShell ebook. In Chapter 13 you find more info on Regular Expressions
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much Lucd.
Yes used and worked in 2020 :smileygrin:, Thank you LucD.
It shure does.
A shame that VMware is not able to put such simple and really necessary information in their webclient.
i would have expected that as basic in 2022.
The column is already there but empty ...
The Length property of each Childitem (file item in the VM folder) does not appear to match the size on disk (which is thin provisioned in my case). It does not appear to match the allocated size of the VM's disk either.
PSChildName Length
----------- ------
xxxx.vmdk 556
xxxx-flat.vmdk 1073741824000
.lck-c30bec3400000000 92
In my case, the vCenter datastore view shows only a single vmdk file of ~35GB (35,036,184 KB). In my case, this VM has disks on multiple datastores, and this datastore only has one of the disks, so the other typical supporting files (vmx, log, etc.) are not included. Is the vCenter view misleading? Is the Powershell data just not accurate? Does my example introduce additional factors not accounted for in this method?
What are you comparing the value with?
For VMDK, which consists of more than just a -flat file, you might have a look at yadr – A vdisk reporter.
Ir reports the sizes for Thin provisioned VMDK correctly.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference