VMware Cloud Community
justg30
Contributor
Contributor
Jump to solution

listing Vcenter, cluster name, datastore details, exported to csv

I'd like to be able to list the datastore details for 12 vcenters (vcenter name, datastore name, free space in GB, used in GB, total GB) but I'm new to powershell. I've found plenty of scripts that lists similar information but without the Vcenter. Does anyone know where I can find a script for this?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this

foreach($vc in $defaultVIServers){
    Get-Datastore -Server $vc | 
   
Select @{N="vCenter";E={$vc.Name}},
       Name,
       CapacityMB,
       FreeSpaceMB,
       @{N
="UsedSpaceMB";E={$_.CapacityMB - $_.FreeSpaceMB}} }


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this

foreach($vc in $defaultVIServers){
    Get-Datastore -Server $vc | 
   
Select @{N="vCenter";E={$vc.Name}},
       Name,
       CapacityMB,
       FreeSpaceMB,
       @{N
="UsedSpaceMB";E={$_.CapacityMB - $_.FreeSpaceMB}} }


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Seems I missed some of your requirements in the title of the post.

Try this

$report = @()
foreach($vc in $defaultVIServers){
    foreach($cluster in (Get-Cluster -Server $vc)){
        Get-Datastore -VMHost (Get-VMHost -Location $cluster) | %{
            $row = "" | Select vCenter,Cluster,Datastore,CapacityMB,FreeSpaceMB,UsedSpaceMB
            $row.vCenter=$vc.Name
            $row.Cluster=$cluster.Name
            $row.Datastore= $_.Name
            $row.CapacityMB=$_.CapacityMB
            $row.FreeSpaceMB=$_.FreeSpaceMB
            $row.UsedSpaceMB=$_.CapacityMB - $_.FreeSpaceMB
            $report += $row
        }     } } $report | Export-Csv "C:\ds-report.csv" -NoTypeInformation -UseCulture


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

justg30
Contributor
Contributor
Jump to solution

Hmmm I get some errors when I try to use that snippet:

Get-Datastore : Cannot bind parameter 'VMHost'. Cannot convert the "" value of type "System.Management.Automation.PSCus
tomObject" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost".
At C:\scripts\datastores\test.ps1:20 char:30
+         Get-Datastore -VMHost <<<<  (Get-VMHost -Location $cluster) | %{
    + CategoryInfo          : InvalidArgument: (:) [Get-Datastore], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDatastore

0 Kudos
LucD
Leadership
Leadership
Jump to solution

WHich PowerCLI version are you using ?

Get-PowerCLIVersion

I tested the script in PowerCLI 5.


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

0 Kudos
justg30
Contributor
Contributor
Jump to solution

PowerCLI Version
----------------
   VMware vSphere PowerCLI 4.1 U1 build 332441
---------------
Snapin Versions
---------------
   VMWare vSphere PowerCLI 4.1 U1 build 332441

0 Kudos
justg30
Contributor
Contributor
Jump to solution

I updated to version 5 but I get the same errors.. on the highlighted row

Add-PSsnapin VMware.VimAutomation.Core  -ErrorAction SilentlyContinue

$defaultVIServers = @("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
Connect-VIServer $defaultVIServers

$report = @()
foreach($vc in $defaultVIServers){
    foreach($cluster in (Get-Cluster -Server $vc)){
       Get-Datastore -VMHost (Get-VMHost -Location $cluster) | %{
           
$row = "" | Select vCenter,Cluster,Datastore,CapacityMB,FreeSpaceMB,UsedSpaceMB
            $row.vCenter=$vc.Name
            $row.Cluster=$cluster.Name
            $row.Datastore= $_.Name
            $row.CapacityMB=$_.CapacityMB
            $row.FreeSpaceMB=$_.FreeSpaceMB
            $row.UsedSpaceMB=$_.CapacityMB - $_.FreeSpaceMB
            $report += $row        }
    }
}


$report | Export-Csv ".\report.csv" -NoTypeInformation -UseCulture

Disconnect-VIServer * -confirm:$false -force:$true

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The variable $defaultVIServers is a pre-defined variable.

It holds all the connections (Connect-VIServer) you have opened.

Perhaps try using another variablename for that.

$myVC = @("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
Connect-VIServer $myVC
...

And you are working in mult-mode I assume ?

Do a

Get-PowerCLIConfiguration


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

justg30
Contributor
Contributor
Jump to solution

I see. Let me adjust the variable on the array.

Proxy Policy    Default Server
                Mode
------------    ---------------
UseSystemProxy  Multiple

That did it! Thanks! Do you think it would be possible to add what the datastore resides on IE. find out if it is on a Netapp vs. a VMMax.. etc? I might add another post to discuss that as a seperate issue.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be possible.

From the datastore extent one can find the corresponding Lun and from there the model.


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

0 Kudos