VMware Cloud Community
esarakaitis
Enthusiast
Enthusiast

Here is a PowerShell script to list datastore information per cluster

$clusters = get-cluster
aforeach ($cluster in $clusters)
{
    $vmhosts = $cluster | get-vmhost
    $firsthost = $vmhosts[0]
    $datastores = $firsthost | get-datastore | % {(Get-View $_.ID).summary}
    foreach ($datastore in $datastores)
    {
        $datastore | select-object @{Name = "Date"; Expression = {get-date}}, @{Name = "Cluster"; Expression = {$cluster.name}}, URL, Name, Freespace, Capacity
           }
        }

http://www.vmwarescripting.com

0 Kudos
3 Replies
SLGizmo
Contributor
Contributor

Can this be exported to a specific location? and in what formats?

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can output the results of a PowerCLI script in various ways like text, csv or html. It is also posible to specify the location. Take a look at the Out-File, Export-Csv or ConvertTo-Html cmdlets with Get-Help.

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

Most excellent you just cut my Search and Find efforts by a few hours as I don't have to type all this in now Smiley Happy I was able to do a manual command get-datastore and output that in the CSV format and get what I need for now.  However, when running the script I get the following error.

Cannot index into a null array.
At <script_location>\list_datastore_info_per_cluster.ps1:6 char:27
+     $firsthost = $vmhosts[ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
The term 'get-datastore' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the nam
e, or if a path was included, verify that the path is correct and try again.
At <script_location>\list_datastore_info_per_cluster.ps1:7 char:45
+     $datastores = $firsthost | get-datastore <<<<  | % {(Get-View $_.ID).summary}
    + CategoryInfo          : ObjectNotFound: (get-datastore:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Can you shed some light on this? - got it.  I was attempting to run this from within Quest PowerGUI script editor and it did not know about the methods of vSphere.  When I open the VMware vSphere PowerCLI and run the ps1 script from within that it all works.  ha... Newbie I am.

0 Kudos