VMware Cloud Community
Vel_VMware
Enthusiast
Enthusiast

Datastore details with cluster name.

Hi

I have written the below script, but don't how to add and get the cluster name with respective to the datastore, also need to add sum of UsableFreeSpace in cluster wise.

Any help would be appreciable please.

$report = @()

Connect-VIServer -Server VCServer

$date = Get-Date -Format 'yyyyMMdd'

$datastores = Get-Datastore | where {$_.Type -eq "VMFS"}

foreach($store in $datastores){

$storeinfo = "" | Select DatastoreName, CapacityGB, FreeSpaceGB, FreeSpaceInPercent, UsableFreeSpace

$datastoreview = get-datastore -Name $store.Name | Get-View | select -ExpandProperty summary

$CapacityGB = [math]::round($datastoreview.Capacity/1GB,2)

$FreeSpaceGB = [math]::round($datastoreview.FreeSpace/1GB,2)

$Percent = [math]::round(($datastoreview.FreeSpace/$datastoreview.Capacity) * 100)

$Usable = [math]::round((($datastoreview.FreeSpace) - ($datastoreview.Capacity/2))/1GB,2)

$storeinfo.DatastoreName = $store.Name

$storeinfo.CapacityGB = $CapacityGB

$storeinfo.FreeSpaceGB = $FreeSpaceGB

$storeinfo.FreeSpaceInPercent = $Percent

$storeinfo.UsableFreeSpace = if($Percent -ge 55) { $Usable } else { 'Not enough space'}

$report += $storeinfo

}

$report | Export-CSV -Path "D:\Datastore_Report_$date.csv" -NoTypeInformation -UseCulture

Disconnect-VIServer -Server VCServer -Confirm:$false

Thanks in advance,

Reply
0 Kudos
6 Replies
jpsider
Expert
Expert

$report = @()


Connect-VIServer -Server VCServer


$date = Get-Date -Format 'yyyyMMdd'


$DsClusters = Get-DatastoreCluster


foreach ($DSCluster in $DsClusters){

    $datastores = Get-Datastore | where {$_.Type -eq "VMFS"}


    foreach($store in $datastores){

        $storeinfo = "" | Select DatastoreName, CapacityGB, FreeSpaceGB, FreeSpaceInPercent, UsableFreeSpace

        $datastoreview = get-datastore -Name $store.Name | Get-View | select -ExpandProperty summary

        $CapacityGB = [math]::round($datastoreview.Capacity/1GB,2)

        $FreeSpaceGB = [math]::round($datastoreview.FreeSpace/1GB,2)

        $Percent = [math]::round(($datastoreview.FreeSpace/$datastoreview.Capacity) * 100)

        $Usable = [math]::round((($datastoreview.FreeSpace) - ($datastoreview.Capacity/2))/1GB,2)


        $storeinfo.DatastoreName = $store.Name

        $storeinfo.DatastoreClusterName = $DSCluster.Name

        $storeinfo.CapacityGB = $CapacityGB

        $storeinfo.FreeSpaceGB = $FreeSpaceGB

        $storeinfo.FreeSpaceInPercent = $Percent

        $storeinfo.UsableFreeSpace = if($Percent -ge 55) { $Usable } else { 'Not enough space'}

       

        #Not sure where in the report you'd want this.

        $totalUseableFreeSpace += $Usable

       

        $report += $storeinfo


    }

}

$report | Export-CSV -Path "D:\Datastore_Report_$date.csv" -NoTypeInformation -UseCulture

Disconnect-VIServer -Server VCServer -Confirm:$false

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Its not getting appropriate cluster name with respective to the datastores.

$totalUseableFreeSpace is like adding all usable freespace in clusterwise and providing sum value.

Reply
0 Kudos
jpsider
Expert
Expert

DOH - Still not sure where in the report you want the cluster free space.  updated below:

$report = @()

Connect-VIServer -Server VCServer

$date = Get-Date -Format 'yyyyMMdd'

$DsClusters = Get-DatastoreCluster

foreach ($DSCluster in $DsClusters){

    $dsclusterName = $DSCluster.Name

    $datastores = Get-DatastoreCluster $dsclusterName | Get-Datastore | where {$_.Type -eq "VMFS"}

    $ClusterFreeSpace = 0


    foreach($store in $datastores){

        $storeinfo = "" | Select DatastoreName, CapacityGB, FreeSpaceGB, FreeSpaceInPercent, UsableFreeSpace

        $datastoreview = get-datastore -Name $store.Name | Get-View | select -ExpandProperty summary

        $CapacityGB = [math]::round($datastoreview.Capacity/1GB,2)

        $FreeSpaceGB = [math]::round($datastoreview.FreeSpace/1GB,2)

        $Percent = [math]::round(($datastoreview.FreeSpace/$datastoreview.Capacity) * 100)


        $Usable = [math]::round((($datastoreview.FreeSpace) - ($datastoreview.Capacity/2))/1GB,2)

        $storeinfo.DatastoreName = $store.Name

        $storeinfo.DatastoreClusterName = $DSCluster.Name

        $storeinfo.CapacityGB = $CapacityGB

        $storeinfo.FreeSpaceGB = $FreeSpaceGB

        $storeinfo.FreeSpaceInPercent = $Percent

        $storeinfo.UsableFreeSpace = if($Percent -ge 55) { $Usable } else { 'Not enough space'}


        #Not sure where in the report you'd want this.

        $ClusterFreeSpace += $Usable


        $report += $storeinfo


    }


}


$report | Export-CSV -Path "D:\Datastore_Report_$date.csv" -NoTypeInformation -UseCulture


Disconnect-VIServer -Server VCServer -Confirm:$false

Reply
0 Kudos
Vel_VMware
Enthusiast
Enthusiast

Hi,

Super, Thanks a lot ... Now its getting appropriate cluster with respective to datastores name. Can you help me with bit more please.

for $totalusable space some thing I want to get report like below

       

DatastoreClusterDatastore NameCapacityGBFreespaceGBFreeSpaceInPercentUsableFreeSpace (>50%)TotalUsableFreeSpace
DatastoreCluster_ADatastoreA_012000.001500.0075500.001500.00
DatastoreA_022000.001500.0075500.00
DatastoreA_032000.001500.0075500.00
DatastoreA_042000.001000.0050Not enough space
DatastoreA_052000.001000.0050Not enough space
DatastoreCluster_BDatastoreB_012000.001000.0050Not enough space1500.00
DatastoreB_022000.001000.0050Not enough space
DatastoreB_032000.001500.0075500.00
DatastoreB_042000.001500.0075500.00
DatastoreB_052000.001500.0075500.00
Reply
0 Kudos
jpsider
Expert
Expert

The problem is that the data is not symetrical.


You are calculating the total cluster available freespace which is technically not part of a single datastore. so the total available space would end up on each line.


$report = @()


Connect-VIServer -Server VCServer

$date = Get-Date -Format 'yyyyMMdd'

$DsClusters = Get-DatastoreCluster


foreach ($DSCluster in $DsClusters){

    $dsclusterName = $DSCluster.Name

    $ClusterFreeSpace = $DSCluster.FreeSpaceGB


    $datastores = Get-DatastoreCluster $dsclusterName | Get-Datastore | where {$_.Type -eq "VMFS"}


    foreach($store in $datastores){

        $storeinfo = "" | Select DatastoreName, CapacityGB, FreeSpaceGB, FreeSpaceInPercent, UsableFreeSpace

        $datastoreview = get-datastore -Name $store.Name | Get-View | select -ExpandProperty summary

        $CapacityGB = [math]::round($datastoreview.Capacity/1GB,2)

        $FreeSpaceGB = [math]::round($datastoreview.FreeSpace/1GB,2)

        $Percent = [math]::round(($datastoreview.FreeSpace/$datastoreview.Capacity) * 100)

        $Usable = [math]::round((($datastoreview.FreeSpace) - ($datastoreview.Capacity/2))/1GB,2)

       

        $storeinfo.DatastoreName = $store.Name

        $storeinfo.DatastoreClusterName = $DSCluster.Name

        $storeinfo.CapacityGB = $CapacityGB

        $storeinfo.FreeSpaceGB = $FreeSpaceGB

        $storeinfo.FreeSpaceInPercent = $Percent

        $storeinfo.UsableFreeSpace = if($Percent -ge 55) { $Usable } else { 'Not enough space'}

        # Added this

        $storeinfo.ClusterFreeSpace = $ClusterFreeSpace


        $report += $storeinfo

    }

}


$report | Export-CSV -Path "D:\Datastore_Report_$date.csv" -NoTypeInformation -UseCulture


Disconnect-VIServer -Server VCServer -Confirm:$false

Vel_VMware
Enthusiast
Enthusiast

Super. Thank you....

Reply
0 Kudos