VMware Cloud Community
Devrawat2708
Contributor
Contributor
Jump to solution

Datastore usage report based on cluster ( not Datacenter )

Hello,

I wish to create separate HTML reports for each cluster I have in my virtual center. I have created a script but it doesnt seem to be working. This script does create separate HTML outputs based on the clusters however all the files have the same data i.e. all the datastores which are available in the Vcenter. How do I separate them with respect to the cluster in which they are assigned instead?

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

# Functions for mathematical operations.

function usedspace{

    param($datastore)

    [math]::Round(($datastore.CapacityMB - $datastore.FreeSpaceMB)/1024,2)

}

function dscapacity{

    param($datastore)

    [math]::Round($datastore.CapacityMB/1024,2)

}

function freespace{

    param($datastore)

    [math]::Round($datastore.FreeSpaceMB/1024,2)

}

function percent{

    param($datastore)

    [math]::Round((($datastore.FreeSpaceMB/1024)/($datastore.CapacityMB/1024)*100)/1,2)

}

#Connect to Vcenter

connect-viserver -server <myservername>

# CSS stylesheet

$a = "<style>"

$a = $a + "BODY{background-color:Gainsboro;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"

$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:SkyBlue}"

$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:PaleTurquoise}"

$a = $a + "*{font-family: Verdana, Arial, Helvetica, sans-serif; font-size: small;}"

$a = $a + "</style>"

# get a list of the clusters

$clusters = get-cluster

# Create HTML report for each cluster

foreach ($cluster in $clusters )

{

$datastores = Get-Datastore | where { $_.name -notcontains "local"} | sort Name

$Report = @()

ForEach ($datastore in $datastores){

        $row = "" | select-object Datastore, Datacenter, CapacityGB, UsedGB, FreeSpaceGB, PercentFree

        $row.Datastore = $datastore.Name

  $row.Datacenter = $datastore.Datacenter

        $row.CapacityGB = dscapacity $datastore

        $row.UsedGB = usedspace $datastore

        $row.FreeSpaceGB = freespace $datastore

  $row.PercentFree = percent $datastore

        $Report += $row

  }

$Report |sort-object -Property PercentFree| ConvertTo-Html -head $a | Set-Content "D:\VMware\Scripts\Reports\Storage\$cluster.html"

}

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

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To retrieve the datastores per cluster, you have to modify the line:

$datastores = Get-Datastore | where { $_.name -notcontains "local"} | sort Name

into:

$datastores = $cluster | Get-Datastore | where { $_.name -notcontains "local"} | sort Name

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

To retrieve the datastores per cluster, you have to modify the line:

$datastores = Get-Datastore | where { $_.name -notcontains "local"} | sort Name

into:

$datastores = $cluster | Get-Datastore | where { $_.name -notcontains "local"} | sort Name

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
Devrawat2708
Contributor
Contributor
Jump to solution

Thanks a lot. Works great.

One more thing..

This line does not filter out my local ESXI datastores. any idea why?

$datastores = $cluster | Get-Datastore | where { $_.name -notcontains "local"}

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

-notcontains is for finding elements in arrays.

You can try:

$datastores = $cluster | Get-Datastore | where { $_.name -notlike "*local*"}

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos