VMware Cloud Community
JonBelanger2011
Enthusiast
Enthusiast
Jump to solution

Datastore Report (CSV)

Hey guys,

Looking for help from a PowerCLI master...

I would like a script that would list all datastores for multiples vCenters in a csv format.

The columns would be Datastore Name, Capacity (GB), Provisionned Space (GB), Free Space (GB), Free Space (%), Host, Cluster, vCenter

I found one that exports it in HTML that is pretty good but I would like to have it in a CSV.

(Script Get DataStore Usage Report on email. )

Let me know if you can help me!

Thanks!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = foreach($vc in $global:DefaultVIServers){

    Get-Datastore -Server $vc | %{

        New-Object PSObject -Property ([ordered]@{

            vCenter = $vc.Name

            ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'

            Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name

            Name = $_.Name

            CapacityGB = $_.CapacityGB

            FreeSpaceGB = $_.FreeSpaceGB

            ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)

            UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)

            FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)

        })

    }

}

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


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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = foreach($vc in $global:DefaultVIServers){

    Get-Datastore -Server $vc | %{

        New-Object PSObject -Property ([ordered]@{

            vCenter = $vc.Name

            ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'

            Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name

            Name = $_.Name

            CapacityGB = $_.CapacityGB

            FreeSpaceGB = $_.FreeSpaceGB

            ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)

            UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)

            FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)

        })

    }

}

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


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

imcooltanmay
Enthusiast
Enthusiast
Jump to solution

Hello LucD

Where we have to use viserver-connect command.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You use Connect-VIServer at the start of the script (1st line).


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

imcooltanmay
Enthusiast
Enthusiast
Jump to solution

Thank you LucD

Created below script

$time_date = Get-Date -Format g

Start-Transcript -Path "D:\report $(get-date -f MM-dd-yyyy_HH_mm_ss).txt"

#Put vcenter list

$vcs = get-content D:\vcenterlist.txt

Foreach ($vc in $vcs)

{

Connect-VIServer $vc

$report = Get-Datastore -Server $vc | %{

         New-Object PSObject -Property ([ordered]@{

            vCenter = $vc

         

            ESXi = (Get-VMHost -Datastore $_ -Server $vc | %{$_.Name.Split('.')[0]}) -join '|'

            Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Cluster | Select -ExpandProperty Name

    

            Name = $_.Name

            CapacityGB = $_.CapacityGB

            FreeSpaceGB = $_.FreeSpaceGB

            ProvisionedGB = $_.CapacityGB - $_.FreeSpaceGB + ($_.ExtensionData.Summary.Uncommitted/1GB)

            UsedGB = ($_.CapacityGB - $_.FreeSpaceGB)

            FreePerc = "{0:P}" -f (1 - ($_.CapacityGB - $_.FreeSpaceGB)/$_.CapacityGB)

        })

    }

$report | Export-Csv -Path "D:\$vc $(get-date -f MM-dd-yyyy_HH_mm_ss).csv" -NoTypeInformation -UseCulture

#disconnect vcenter.

disconnect-viserver $vc -confirm:$false

}

Stop-Transcript

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

After running the script it stuck forever

pastedImage_1.png

Thanks

vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Without any further information that is hard to diagnose I'm afraid.

How many vCenters, datastores... are involved?
Did you try to run it against a smaller sample (1 VC, a few datastores)?

Is it really stuck? Is the script still using CPU?
Did you try hitting <Enter> after the Warning message?

Did you stop/start you PS session before trying again?


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Yes, after couple of minutes

At D:\vmk\Datastore-freespace.ps1:11 char:23

+             Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Clus ...

+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-VMHost], ViError

    + FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle

   ts.Commands.GetVMHost

Get-VMHost : 12/23/2019 4:43:35 PM      Get-VMHost              The HTTP service located at https://vc-

.com/sdk

is unavailable.  This could be because the service is too busy or because no endpoint was found listening at the

specified address. Please ensure that the address is correct and try accessing the service again later.

At D:\vmk\Datastore-freespace.ps1:11 char:23

+             Cluster = Get-VMHost -Datastore $_ -Server $vc | Get-Clus ...

+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-VMHost], ViError

    + FullyQualifiedErrorId : Client20_QueryServiceImpl_RetrievePropertiesEx_ViError,VMware.VimAutomation.ViCore.Cmdle

   ts.Commands.GetVMHost

Thanks

vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like there is an issue with one of the vCenters.
Can you do the following against al the vCenters, without an error?

Get-Datastore | Get-VMHost


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Yes, it fetching ESXi host.

Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version

Thanks

vmk

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Any errors on this?

foreach($vc in $global:defaultviservers){

    Get-Datastore -Server $vc | Get-VMHost -Server $vc

}


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

Reply
0 Kudos