PowerCLI

 View Only
  • 1.  DataStore Folder & Size Details

    Posted Aug 02, 2016 08:06 AM

    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



  • 2.  RE: DataStore Folder & Size Details
    Best Answer

    Posted Aug 02, 2016 11:51 AM

    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

    }



  • 3.  RE: DataStore Folder & Size Details

    Posted Aug 03, 2016 03:35 AM

    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



  • 4.  RE: DataStore Folder & Size Details

    Posted Aug 03, 2016 04:58 AM

    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



  • 5.  RE: DataStore Folder & Size Details

    Posted Aug 03, 2016 06:04 AM

    Thank you so much Lucd.



  • 6.  RE: DataStore Folder & Size Details

    Broadcom Employee
    Posted Oct 08, 2020 06:23 AM

    Yes used and worked in 2020 :smileygrin:, Thank you LucD.



  • 7.  RE: DataStore Folder & Size Details

    Posted Sep 20, 2022 11:43 AM

    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 ...

    WuGeDe_0-1663674401050.png

     



  • 8.  RE: DataStore Folder & Size Details

    Posted Oct 17, 2022 06:28 PM

    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?



  • 9.  RE: DataStore Folder & Size Details

    Posted Oct 17, 2022 06:37 PM

    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.