VMWare: Get basic datastore information

This script will take a name of a specific datastore, a list of datastores, or if none is specified query vSphere for ALL datastores, and will return an object with some basic information.

As of now the information you will receive back is

Name (of the datastore)
Capacity (in GB)
Free Space (in GB)
Free Space (in percent)
Whether datastore is accessible or not

Please, as always, let me know if you find any errors or have suggestions for improvements.

[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)]$Datastore)

begin{
if ((Get-PSSnapin -Name VMWare.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null){Add-PSSnapin VMWare.VimAutomation.Core}
Connect-VIServer -Server HCA-MSYS63 -WarningAction SilentlyContinue|Out-Null
$tmpDatastore = @()
}

process{$tmpDatastore += $Datastore}

end{
if ($Datastore){
foreach ($ds in $tmpDatastore){
$dsi = Get-View -ViewType Datastore -Property Summary -Filter @{"Summary.Name"=$ds}

$dsName = $dsi.Summary.Name
$dsCapacityGB = [System.Convert]::ToDecimal((($dsi.Summary.Capacity)/1GB).ToString("#.00"))
$dsFreespaceGB = [System.Convert]::ToDecimal((($dsi.Summary.Freespace)/1GB).ToString("#.00"))
$dsPercentFree = [System.Convert]::ToDecimal((($dsi.Summary.Freespace/$dsi.Summary.Capacity)*100).ToString("#.00"))
$dsAccessible = $dsi.Summary.Accessible

$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $dsName -PassThru |
Add-Member -MemberType NoteProperty -Name CapacityGB -Value $dsCapacityGB -PassThru |
Add-Member -MemberType NoteProperty -Name FreespaceGB -Value $dsFreespaceGB -PassThru |
Add-Member -MemberType NoteProperty -Name PercentFree -Value $dsPercentFree -PassThru |
Add-Member -MemberType NoteProperty -Name Accessible -Value $dsAccessible
Write-Output $obj}

}else{

foreach ($ds in (Get-View -ViewType Datastore -Property Summary)){
$dsName = $ds.Summary.Name
$dsCapacityGB = [System.Convert]::ToDecimal((($ds.Summary.Capacity)/1GB).ToString("#.00"))
$dsFreespaceGB = [System.Convert]::ToDecimal((($ds.Summary.Freespace)/1GB).ToString("#.00"))
$dsPercentFree = [System.Convert]::ToDecimal((($ds.Summary.Freespace/$ds.Summary.Capacity)*100).ToString("#.00"))
$dsAccessible = $ds.Summary.Accessible

$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $dsName -PassThru |
Add-Member -MemberType NoteProperty -Name CapacityGB -Value $dsCapacityGB -PassThru |
Add-Member -MemberType NoteProperty -Name FreespaceGB -Value $dsFreespaceGB -PassThru |
Add-Member -MemberType NoteProperty -Name PercentFree -Value $dsPercentFree -PassThru |
Add-Member -MemberType NoteProperty -Name Accessible -Value $dsAccessible
Write-Output $obj}}}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s