VMware Cloud Community
vmstoani
Contributor
Contributor
Jump to solution

Datastore CapacityGB and FreeSpaceGB

Hi

I need the current capacity for a new infrastructure platform. But I can’t export the values ​​for Datastore CapacityGB and FreeSpaceGB.

Hope someone can help me.

Here's the Script:

Connect-VIServer -server xxx -User xxx -Password xxx

$VMs = Get-Cluster -Name xxx | Get-VM

$Output = foreach ($VM in $VMs){

Get-VM $VM | select Name,

@{N=”Folder Name”;E={$_.Folder.Name}},

@{N=”Number vCPU”;E={$_.NumCpu}},

@{N=”vMem(GB)”;E={$_.MemoryGB}},

@{N=”CPU Usage(MHz)”;E={$_.ExtensionData.Summary.QuickStats.OverallCpuUsage}},

@{N=”Memory Usage(MB)”;E={$_.ExtensionData.Summary.QuickStats.GuestMemoryUsage}},

@{N=”Provisioned Space(GB)”;E={$_.ProvisionedSpaceGB}},

@{N=”Used Space(GB)”;E={$_.UsedSpaceGB}},

@{N=”Power State”;E={$_.PowerState}},

@{N=”ESXi Host”;E={$_.VMHost}},

@{N=”Cluster”;E={Get-Cluster -VM $_}},

@{N=”Datastore”;E={$_.ExtensionData.Config.DatastoreUrl.Name}},

@{N=”Datastore Capacity(GB)”;E={Get-Datastore -VM $_ | Select CapacityGB}}, -> doesn't work

@{N=”Datastore Free Space(GB)”;E={Get-Datastore -VM $_. | Select FreeSpaceGB}} -> doesn't work

#@{N=”DNSName”;E={$_.ExtensionData.Guest.Hostname}}

}

$Output | Export-Csv D:\Result.csv -NoTypeInformation

Best regards

Andi

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When you just want the value of a property on a Select-Object, you can use the ExpandProperty parameter.

Also, there is no need to do a ForEach here, you can do it all in one pipeline construct.

Something like this for example

Connect-VIServer -server xxx -User xxx -Password xxx

Get-Cluster -Name xxx | Get-VM |

select Name,

    @{N=”Folder Name”;E={$_.Folder.Name}},

    @{N=”Number vCPU”;E={$_.NumCpu}},

    @{N=”vMem(GB)”;E={$_.MemoryGB}},

    @{N=”CPU Usage(MHz)”;E={$_.ExtensionData.Summary.QuickStats.OverallCpuUsage}},

    @{N=”Memory Usage(MB)”;E={$_.ExtensionData.Summary.QuickStats.GuestMemoryUsage}},

    @{N=”Provisioned Space(GB)”;E={$_.ProvisionedSpaceGB}},

    @{N=”Used Space(GB)”;E={$_.UsedSpaceGB}},

    @{N=”Power State”;E={$_.PowerState}},

    @{N=”ESXi Host”;E={$_.VMHost}},

    @{N=”Cluster”;E={Get-Cluster -VM $_}},

    @{N=”Datastore”;E={$_.ExtensionData.Config.DatastoreUrl.Name}},

    @{N=”Datastore Capacity(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty CapacityGB}},

    @{N=”Datastore Free Space(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty FreeSpaceGB}},

    @{N=”DNSName”;E={$_.ExtensionData.Guest.Hostname}} |

Export-Csv D:\Result.csv -NoTypeInformation


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

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

When you just want the value of a property on a Select-Object, you can use the ExpandProperty parameter.

Also, there is no need to do a ForEach here, you can do it all in one pipeline construct.

Something like this for example

Connect-VIServer -server xxx -User xxx -Password xxx

Get-Cluster -Name xxx | Get-VM |

select Name,

    @{N=”Folder Name”;E={$_.Folder.Name}},

    @{N=”Number vCPU”;E={$_.NumCpu}},

    @{N=”vMem(GB)”;E={$_.MemoryGB}},

    @{N=”CPU Usage(MHz)”;E={$_.ExtensionData.Summary.QuickStats.OverallCpuUsage}},

    @{N=”Memory Usage(MB)”;E={$_.ExtensionData.Summary.QuickStats.GuestMemoryUsage}},

    @{N=”Provisioned Space(GB)”;E={$_.ProvisionedSpaceGB}},

    @{N=”Used Space(GB)”;E={$_.UsedSpaceGB}},

    @{N=”Power State”;E={$_.PowerState}},

    @{N=”ESXi Host”;E={$_.VMHost}},

    @{N=”Cluster”;E={Get-Cluster -VM $_}},

    @{N=”Datastore”;E={$_.ExtensionData.Config.DatastoreUrl.Name}},

    @{N=”Datastore Capacity(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty CapacityGB}},

    @{N=”Datastore Free Space(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty FreeSpaceGB}},

    @{N=”DNSName”;E={$_.ExtensionData.Guest.Hostname}} |

Export-Csv D:\Result.csv -NoTypeInformation


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

Reply
0 Kudos
vmstoani
Contributor
Contributor
Jump to solution

Hi Luc,

Thank you so much. It works.

Nice Sunday

ANDI

Reply
0 Kudos
mahady_hassan
Enthusiast
Enthusiast
Jump to solution

hey Luc i am running below script for datastore usage percentage. in the capacityGB and FreeSpaceGB  it doesnt give value like floating point 3. can you help in this

 

#Get Datastore usage percentage

# Import the PowerCLI module into your PowerShell session
Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue

# Get the list of vCenter darastores
$datastores = Get-Datastore
$ErrorActionPreference = 'SilentlyContinue'
# loop through all available datastores
ForEach ($datastore in $datastores)
{
# Find the size of the committed space of all thin disks in a datastore (how much space it is required if all vmdk files will grow to the sizes specified in their settings)
$Provision = ([Math]::Round(($datastore.ExtensionData.Summary.Capacity - $datastore.ExtensionData.Summary.FreeSpace + $datastore.ExtensionData.Summary.Uncommitted)/1GB,3))
# Percentage of free space in the datastore
$PerFree = ([math]::Round(($datastore.FreeSpaceGB)/($datastore.CapacityGB)*100, 3))
# Percentage of thin disk overcommitment
$PerOvercommit = ([math]::Round($Provision/($datastore.CapacityGB)*100,3))
# Add extra properties to the datastore object
$datastore | Add-Member -type NoteProperty -name PercentsFree -value $PerFree
$datastore | Add-Member -type NoteProperty -name CapacityGb -value ([Math]::Round(($datastore.ExtensionData.Summary.Capacity)/1GB,3))
$datastore | Add-Member -type NoteProperty -name FreeSpaceGb -value ([Math]::Round(($datastore.ExtensionData.Summary.FreeSpace)/1GB,3))
$datastore | Add-Member -type NoteProperty -name ProvisionedSpaceGb -value $Provision
$datastore | Add-Member -type NoteProperty -name PercentsOvercommit -value $PerOvercommit
}

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean.
Can you show an example of what you get and what you want to get?


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

Reply
0 Kudos
mahady_hassan
Enthusiast
Enthusiast
Jump to solution

Spoiler
Sure below script i am running
=============================

# Get the list of vCenter darastores
$datastores = Get-Datastore
$ErrorActionPreference = 'SilentlyContinue'
# loop through all available datastores
ForEach ($datastore in $datastores)
{
# Find the size of the committed space of all thin disks in a datastore (how much space it is required if all vmdk files will grow to the sizes specified in their settings)
$Provision = ([Math]::Round(($datastore.ExtensionData.Summary.Capacity - $datastore.ExtensionData.Summary.FreeSpace + $datastore.ExtensionData.Summary.Uncommitted)/1GB,0))
# Percentage of free space in the datastore
$PerFree = ([math]::Round(($datastore.FreeSpaceGB)/($datastore.CapacityGB)*100,2))
# Percentage of thin disk overcommitment
$PerOvercommit = ([math]::Round($Provision/($datastore.CapacityGB)*100,2))
# Add extra properties to the datastore object
$datastore | Add-Member -type NoteProperty -name PercentsFree -value $PerFree
$datastore | Add-Member -type NoteProperty -name CapacityGb -value ([Math]::Round(($datastore.ExtensionData.Summary.Capacity)/1GB,0))
$datastore | Add-Member -type NoteProperty -name FreeSpaceGb -value ([Math]::Round(($datastore.ExtensionData.Summary.FreeSpace)/1GB,0))
$datastore | Add-Member -type NoteProperty -name ProvisionedSpaceGb -value $Provision
$datastore | Add-Member -type NoteProperty -name PercentsOvercommit -value $PerOvercommit
}
# Display the resulting data on VMWare datastores
$frag8= $datastores|select-object Name, Type, Datacenter,CapacityGb,FreeSpaceGb,PercentsFree,ProvisionedSpaceGb,PercentsOvercommit|
sort PercentsFree| convertto-html -fragment -as table -precontent "<h2>Datastore Usage Percentage</h2>" -postcontent '<i>' | out-string

====================

Output

mahady_hassan_0-1678355608529.png

 i want to get 3 decimal point for freespaceGB but it doesnt working can you help

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing that line to

$datastore | Add-Member -type NoteProperty -name FreeSpaceGb -value ([Math]::Round(($datastore.ExtensionData.Summary.FreeSpace)/1GB,3))


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

Reply
0 Kudos
mahady_hassan
Enthusiast
Enthusiast
Jump to solution

tried this by changing value 2, 3, 4 but this doesnt work at all

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, you are trying to change properties on a read-only object, but you don't get the error due to the $ErrorActionPreference you set.

I would do this in this way

# Get the list of vCenter datastores
$frag8 = Get-Datastore |
Select-Object Name, Type, Datacenter,
@{N='CapacityGb';E={[math]::Round($_.CapacityGB,0)}},
@{N = 'FreeSpaceGb'; E = { [Math]::Round(($_.ExtensionData.Summary.FreeSpace) / 1GB, 3)} },
@{N = 'PercentsFree'; E = { [math]::Round(($_.FreeSpaceGB) / ($_.CapacityGB) * 100, 3)}},
@{N = 'ProvisionedSpaceGb'; E = { [Math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted) / 1GB, 0)} },
@{N = 'PercentsOvercommit'; E = { [math]::Round(($_.ExtensionData.Summary.Capacity - $_.ExtensionData.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted) / ($_.CapacityGB*1GB) * 100, 1) } } |
Sort-Object -Property PercentsFree |
ConvertTo-Html -Fragment -As table -PreContent "<h2>Datastore Usage Percentage</h2>" -PostContent '<i>' |
Out-String


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

Reply
0 Kudos
mahady_hassan
Enthusiast
Enthusiast
Jump to solution

this works fine for me. thanks a lot man.  i want to add percent in the output table information in percentsfree and percent overcommit column data

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Those 2 properties are in the table


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

Reply
0 Kudos