VMware Cloud Community
the1960
Contributor
Contributor

VM-Diskspace per Folder

Hello

Maybe someone built a script to calculate the sum of the occupied disk space of the VM folder?

Best regards

0 Kudos
10 Replies
LucD
Leadership
Leadership

Is that provisioned or used, or both?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

Never mind, I included both.
You can always leave out the one you don't want.

Get-Folder -Type VM |

   ForEach-Object -Process {

   $vms = Get-VM -Location $_

   New-Object PSObject -Property ([ordered]@{

   Folder = $_.Name

   VMCount = $vms.Count

   TotalProvisionedSpaceGB = [math]::Round(($vms | Measure-Object -Property ProvisionedSpaceGB -Sum).Sum)

   TotalUsedSpaceGB = [math]::Round(($vms | Measure-Object -Property UsedSpaceGB -Sum).Sum)

   })

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
the1960
Contributor
Contributor

Hi LucD,

thanks It's worked great.

Thanks for the quick response and answer.

I'm not the PS-Guru 😉

How can I past in a csv-file.

Bets regards

0 Kudos
LucD
Leadership
Leadership

You mean a CSV file with the names of the folders you want to report on?

It depends if you have folders with the same name in different locations or not.

Do you?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
the1960
Contributor
Contributor

No I think the same name are not in the structure.

But I can't

0 Kudos
the1960
Contributor
Contributor

But I can not rule it out.

sorry

0 Kudos
LucD
Leadership
Leadership

You can use my FolderByPath function.

The script would then look like this

function Get-FolderByPath {

   <# .SYNOPSIS Retrieve folders by giving a path .DESCRIPTION The function will retrieve a folder by it's path. The path can contain any type of leave (folder or datacenter). .NOTES Author: Luc Dekens .PARAMETER Path The path to the folder. This is a required parameter. .PARAMETER Path The path to the folder. This is a required parameter. .PARAMETER Separator The character that is used to separate the leaves in the path. The default is '/' .EXAMPLE PS> Get-FolderByPath -Path "Folder1/Datacenter/Folder2"

.EXAMPLE

PS> Get-FolderByPath -Path "Folder1>Folder2" -Separator '>'

#>

   param(

   [CmdletBinding()]

   [parameter(Mandatory = $true)]

   [System.String[]]${Path},

   [char]${Separator} = '/'

   )

   process {

   if ((Get-PowerCLIConfiguration).DefaultVIServerMode -eq "Multiple") {

   $vcs = $defaultVIServers

   }

   else {

   $vcs = $defaultVIServers[0]

   }

   foreach ($vc in $vcs) {

   foreach ($strPath in $Path) {

   $root = Get-Folder -Name Datacenters -Server $vc

   $strPath.Split($Separator) | % {

   $root = Get-Inventory -Name $_ -Location $root -Server $vc -NoRecursion

   if ((Get-Inventory -Location $root -NoRecursion | Select -ExpandProperty Name) -contains "vm") {

   $root = Get-Inventory -Name "vm" -Location $root -Server $vc -NoRecursion

   }

   }

   $root | where {$_ -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl]}| % {

   Get-Folder -Name $_.Name -Location $root.Parent -NoRecursion -Server $vc

   }

   }

   }

   }

}


# CSV layout

#

# FolderPath

# DC1/Folder2

# DC1/Folder1/Folder2

#


Import-Csv -Path .\foldernames.csv -UseCulture |

ForEach-Object -Process {

   $folder = Get-FolderByPath -Path $_.FolderPath

   $vms = Get-VM -Location $folder

   New-Object PSObject -Property ([ordered]@{

   Folder = $folder.Name

   Path = $_.FolderPath

   VMCount = $vms.Count

   TotalProvisionedSpaceGB = [math]::Round(($vms | Measure-Object -Property ProvisionedSpaceGB -Sum).Sum)

   TotalUsedSpaceGB = [math]::Round(($vms | Measure-Object -Property UsedSpaceGB -Sum).Sum)

   })

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
the1960
Contributor
Contributor

Hi Luc

I tried it myself and it works. Certainly not optimal but the result is right.

$report = @()

Get-Folder -Type VM |

ForEach-Object -Process {

   $row = '' | Select-Object Folder, VMCount, ProvisionedSpace, TotalUsedSpace

   $vms = Get-VM -Location $_

   New-Object PSObject -Property ([ordered]@{

   Folder .                                 = $_.Name

      VMCount                           = $vms.Count

      TotalProvisionedSpaceGB = [math]::Round(($vms | Measure-Object -Property ProvisionedSpaceGB -Sum).Sum)

      TotalUsedSpaceGB           = [math]::Round(($vms | Measure-Object -Property UsedSpaceGB -Sum).Sum)

   })

   $row.Folder = $_.Name

   $row.VMCount = $vms.Count

   $row.ProvisionedSpace = [math]::Round(($vms | Measure-Object -Property ProvisionedSpaceGB -Sum).Sum)

   $row.TotalUsedSpace   = [math]::Round(($vms | Measure-Object -Property UsedSpaceGB -Sum).Sum)

  

   $report += $row

}

$report | Export-Csv "C:\Users\adm.t.heise\Documents\VM-Folder_Disk\FolderView.csv" -Delimiter ";" -NoTypeInformation

what do you thinks?

0 Kudos
LucD
Leadership
Leadership

I obviously misunderstood your last question.
I thought you wanted to provide a set of folders to report on in a CSV file.

In any case, since we are placing the result on the pipeline (thanks to ForEach-Object), we can just pipe the result to Export-Csv.

Like this

Get-Folder -Type VM |

ForEach-Object -Process {

   $vms = Get-VM -Location $_

   New-Object PSObject -Property ([ordered]@{

   Folder = $_.Name

   VMCount   = $vms.Count

   TotalProvisionedSpaceGB = [math]::Round(($vms | Measure-Object -Property ProvisionedSpaceGB -Sum).Sum)

   TotalUsedSpaceGB   = [math]::Round(($vms | Measure-Object -Property UsedSpaceGB -Sum).Sum)

   })

} |

Export-Csv "C:\Temp\FolderView.csv" -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
the1960
Contributor
Contributor

Luc,

thanks for your help. Very great.

You helped me a lot. The problem is solved.

Best regards

0 Kudos