VMware Cloud Community
G0nz0UK
Enthusiast
Enthusiast

Powershell script to show Cluster storage usage/free?

Hello,
 
We have 4 clusters in 1 vCenter and I've been asked to create a script to show:
 
total storage size, Used space, Free Space, Provisioned, Uncommitted space for each cluster.
 
Does anyone have a similar script they could share at all?
 
I use this for the datastores, but it's too granular, it would be great to show the cluster name and the above details.
 
 
 

 

Get-Datastore |

Select @{N='Datacenter';E={$_.Datacenter.Name}},

@{N='DSC';E={Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name}},

Name,CapacityGB,@{N='FreespaceGB';E={[math]::Round($_.FreespaceGB,2)}},

@{N='ProvisionedSpaceGB';E={

[math]::Round(($_.ExtensionData.Summary.Capacity - $_.Extensiondata.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)}},

@{N='UnCommittedGB';E={[math]::Round($_.ExtensionData.Summary.Uncommitted/1GB,2)}},

@{N='VM';E={$_.ExtensionData.VM.Count}} |

Export-Csv c:\temp\test.csv -NoTypeInformation -UseCulture

 

 
Thanks
Labels (3)
0 Kudos
37 Replies
LucD
Leadership
Leadership

Try something like this.

Get-Cluster -PipelineVariable cluster |
ForEach-Object -Process {
  $ds = Get-Datastore -RelatedObject $cluster | where{$_.ExtensionData.Summary.MultipleHostAccess}
  $totCapacity, $totFree, $totUncommitted = ($ds.ExtensionData.Summary | Measure-Object -Property Capacity, FreeSpace, Uncommitted -Sum).Sum
  [PSCustomObject]@{
    Cluster = $cluster.Name
    CapacityGB = [math]::Round($totCapacity/1GB,1)
    UsedGB = [math]::Round(($totCapacity - $totFree)/1GB,1)
    FreeGB = [math]::Round($totFree / 1GB, 1)
    ProvisionedGB = [math]::Round(($totCapacity - $totFree + $totUncommitted) / 1GB, 1)
    UncommittedGB = [math]::Round($totUncommitted / 1GB, 1)
  }
}

If you only want to include the datastores that are accessible on multiple nodes replace this line

$ds = Get-Datastore -RelatedObject $cluster

with this line

$ds = Get-Datastore -RelatedObject $cluster | where{$_.ExtensionData.Summary.MultipleHostAccess}

 


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

G0nz0UK
Enthusiast
Enthusiast

This is simply amazing!!  Thanks.

I wonder if you can help with 2 additions to this, I've been trying to get to output to a CSV with this info across this top

Cluster, CapacityGB,UsedGB,FreeGB,ProvisionGB,UncommittedGB

But have failed to get this working.

Also do you have any knowledge on how this wonderful script can be outputted to a SQL table too?  Something I've been asked to try and do.  I've been give a database to test with.  I think it means we can then use other systems to use this data.

No worries if not as you have helped me lots already

 

0 Kudos
LucD
Leadership
Leadership

You use a Select-Object to determine the order of the properties.

Get-Cluster -PipelineVariable cluster |
ForEach-Object -Process {
  $ds = Get-Datastore -RelatedObject $cluster | where{$_.ExtensionData.Summary.MultipleHostAccess}
  $totCapacity, $totFree, $totUncommitted = ($ds.ExtensionData.Summary | Measure-Object -Property Capacity, FreeSpace, Uncommitted -Sum).Sum
  [PSCustomObject]@{
    Cluster = $cluster.Name
    CapacityGB = [math]::Round($totCapacity/1GB,1)
    UsedGB = [math]::Round(($totCapacity - $totFree)/1GB,1)
    FreeGB = [math]::Round($totFree / 1GB, 1)
    ProvisionedGB = [math]::Round(($totCapacity - $totFree + $totUncommitted) / 1GB, 1)
    UncommittedGB = [math]::Round($totUncommitted / 1GB, 1)
  }
} | Select Cluster, CapacityGB,UsedGB,FreeGB,ProvisionGB,UncommittedGB |
Export-Csv -path .\report.csv -UseCulture -NoTypeInformation

To write to an SQL table you could use the Write-SqlTableData cmdlet.
It requires the SqlServer module to be installed.



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

G0nz0UK
Enthusiast
Enthusiast

Amazing thank you so much for your help.  

I've also produced a nice datastore report too, so 2 nice reports.

Converting your script so it send to SQL will be hard, but I need to do this and if I can it will open up many possibilities.

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

This script is great, I'm using it in production now!  

I'm now trying to simplify your script, where I'm trying to get the single values for a given cluster name I specify for example 'cluster123' where I'm trying to get the single values for:

CapacityGB
UsedGB
FreeGB
ProvisionedGB
UncommittedGB

I'm trying to create 5 PS scripts for the above for a given cluster name as I need to then push the results into a DB.

Get-Cluster -Name cluster123 CapacityGB

Get-Cluster -Name cluster123 UsedGB

etc

Is this possible?

0 Kudos
LucD
Leadership
Leadership

You could package the script in a function and add the ClusterName as a parameter to the function.

Something like this for example

function Get-ClusterInfo {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ClusterName,
        [Parameter(Mandatory=$true,ParameterSetName = 'CapacityGB')]
        [switch]$CapacityGB,
        [Parameter(Mandatory=$true, ParameterSetName = 'UsedGB')]
        [switch]$UsedGB,
        [Parameter(Mandatory=$true,ParameterSetName = 'FreeGB')]
        [switch]$FreeGB,
        [Parameter(Mandatory=$true,ParameterSetName = 'ProvisionedGB')]
        [switch]$ProvisionedGB,
        [Parameter(Mandatory=$true,ParameterSetName = 'UncommittedGB')]
        [switch]$UncommittedGB
    )

    $cluster = Get-Cluster -Name $ClusterName
    $ds = Get-Datastore -RelatedObject $cluster | where { $_.ExtensionData.Summary.MultipleHostAccess }
    $totCapacity, $totFree, $totUncommitted = ($ds.ExtensionData.Summary | Measure-Object -Property Capacity, FreeSpace, Uncommitted -Sum).Sum
    if ($CapacityGB) {
        [math]::Round($totCapacity / 1GB, 1)
    } elseif ($UsedGB) {
        [math]::Round(($totCapacity - $totFree) / 1GB, 1)
    } elseif ($FreeGB) {
        [math]::Round($totFree / 1GB, 1)
    } elseif ($ProvisionedGB) {
        [math]::Round(($totCapacity - $totFree + $totUncommitted) / 1GB, 1)
    } elseif ($UncommittedGB) {
        [math]::Round($totUncommitted / 1GB, 1)
    }
}

$clusterName = 'cluster'
Get-ClusterInfo -ClusterName $clusterName -CapacityGB
Get-ClusterInfo -ClusterName $clusterName -UsedGB
Get-ClusterInfo -ClusterName $clusterName -FreeGB
Get-ClusterInfo -ClusterName $clusterName -ProvisionedGB
Get-ClusterInfo -ClusterName $clusterName -UncommittedGB


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Wow thanks!

How could I get it down to the absolute basic for example, how could I just show the capacity if a single command like this?

 

Get-ClusterInfo -ClusterName HollandCluster -CapacityGB

I know this won't work as it need the extra calculations, but what could I use as a small single query for each?

0 Kudos
LucD
Leadership
Leadership

Not sure I understand what you mean


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Sorry I thought that when I replied.

without using variables how’s can get the size or used space of a cluster storage if I knew  what the cluster name was?

If our cluster was called ‘London’ and all I need was it’s used space, what’s the most simplistic query I could use for that?

thanks

 

0 Kudos
LucD
Leadership
Leadership

Completely illegible, and I'm not a fan, but this would work for UsedSpaceGB

[math]::Round(((((Get-Datastore -RelatedObject (Get-Cluster -Name 'London')).where{ $_.ExtensionData.Summary.MultipleHostAccess })).ExtensionData.Summary | %{$_.Capacity - $_.FreeSpace} | Measure-Object -Sum).Sum/1GB,1)

 


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

It might help if I tell you what I'm doing as you are probably thing I can get it in a much better way.

I'm trying to send each value into an InfluxDB (cluster name, usedGB, freeGB etc).  I do this for other systems like getting Citrix stats and I'm trying to do the same for vCenter.

Here is a simple script I use for Citrix, but I want to do the same for vCenter.

I just has 2 lines that give me values that I send to Influx, I wanted to do something similar for each vcenter cluster I have, so for the London Cluster I wanted to run something like below to output the Capacity, used, free, provisioned, uncommitted space on each line.

Import-Module Influx

$user = 'citrixuser'
$file = 'C:\Scripts\pwdfolder.txt'
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, (Get-Content $file | ConvertTo-SecureString)

While ($true){
$Metrics = @{
CitrixInuse = [int](Get-BrokerDesktopGroup -adminaddress "citrixsvr.domain.com" -MaxRecordCount 50000 | where name -EQ "W10_Standard" | Select-Object DesktopsInUse | ForEach-Object {$_.DesktopsInUse})
Citrixfaulty = [int](Get-BrokerDesktopGroup -adminaddress "citrixsvr.domain.com" -MaxRecordCount 50000 | where name -EQ "W10_Standard" | Select-Object DesktopsFaulted | ForEach-Object {$_.DesktopsFaulted})
}
Write-Influx -Measure SvrFolderCount -Tags @{host="citrix"} -Metrics $Metrics -Database "CitrixDB" -Server http://10.1.2.65:8086 -Credential $credentials
Start-Sleep -Seconds 10

 

 

 

 

 

0 Kudos
LucD
Leadership
Leadership

You can just use that sample for UsedSpaceGB like this, and add the other values similarly.

$Metrics = @{
    UsedSpaceGB = [math]::Round(((((Get-Datastore -RelatedObject (Get-Cluster -Name 'London')).where{ $_.ExtensionData.Summary.MultipleHostAccess })).ExtensionData.Summary | %{$_.Capacity - $_.FreeSpace} | Measure-Object -Sum).Sum/1GB,1)
}

 


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

If I run that for London (we have a cluster called London) it runs in PS ISE but not results or error.

Would you expect a value to be returned?

Your other simpler example does return a value though, it's just the last one which I want to try and put into Influx didn't.

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

The value should be in the $Metrics hash table under the UsedpaceGB entry.

At least it is for me.


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Boom! It was a typo my side that value got pumped into the InfluxDB and visible in Grafana!

I'll try the other values tomorrow.  I think it's just the maths you used to get the results I may have issues with, but I should be able to use your amazing master script to pull those out as single statements like you did for this one, well I hope so.

0 Kudos
LucD
Leadership
Leadership

Great, let me know if you encounter issues


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Apologies, I'm getting stuck on the next value - Capacity:

UsedSpaceGB = [math]::Round(((((Get-Datastore -RelatedObject (Get-Cluster -Name 'London')).where{ $_.ExtensionData.Summary.MultipleHostAccess })).ExtensionData.Summary | %{$_.Capacity} | Measure-Object -Sum).Sum/1GB,1)

UsedSpaceGB = [math]::Round(((((Get-Datastore -RelatedObject (Get-Cluster -Name 'London')).where{ $_.ExtensionData.Summary.MultipleHostAccess })).ExtensionData.Summary | %{$_.Capacity} | Measure-Object -Sum).Sum/1GB,1)

I based it on the one that worked you helped with.

I then need to do the remaining ones:

FreeGB = [math]::Round($totFree / 1GB, 1)
ProvisionedGB = [math]::Round(($totCapacity - $totFree + $totUncommitted) / 1GB, 1)
UncommittedGB = [math]::Round($totUncommitted / 1GB, 1)
 

0 Kudos
LucD
Leadership
Leadership

The Name of the hash table entry should be Capacity, instead of UsedSpaceGB.


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

I think I'm being stupid, so I need these you have thankfully helped with:

 

 

CapacityGB = [math]::Round($totCapacity/1GB,1)
UsedGB = [math]::Round(($totCapacity - $totFree)/1GB,1)
FreeGB = [math]::Round($totFree / 1GB, 1)
ProvisionedGB = [math]::Round(($totCapacity - $totFree + $totUncommitted) / 1GB, 1)
UncommittedGB = [math]::Round($totUncommitted / 1GB, 1)

 

 

 

This worked for used space into InfluxDB:


 

 

$Metrics = @{
    LonClusterUsedSpaceGB = [math]::Round(((((Get-Datastore -RelatedObject (Get-Cluster -Name 'London')).where{ $_.ExtensionData.Summary.MultipleHostAccess })).ExtensionData.Summary | %{$_.Capacity - $_.FreeSpace} | Measure-Object -Sum).Sum/1GB,1)
}
Write-Influx -Measure LonClusterUsedSpaceGB -Tags @{host="om"} -Metrics $Metrics -Database "VMwareDB" -Server http://10.1.2.65:8086 -Credential $credentialsinflux
    Start-Sleep -Seconds 10​

 

 


 So was hoping to add the other lines for London like above as 'LonClusterCapatityBG' etc like this:

G0nz0UK_0-1644597161127.png

So five lines for each attribute for that cluster called London, is that possible?

0 Kudos