VMware Cloud Community
lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

Get datastore details

Hello

I have created the below script, my issue is that the result like this

lElOUCHE_79_0-1674139230171.png

 

and I would like to have the output in this order:

  1. Name
  2. Type
  3. path
  4. FreeSpacePercent
  5. FreeSpaceGB

 

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Like I said earlier, use the [ordered] construct.

    # Add the information to the array
    $datastoreInfo += New-Object PSObject -Property ([ordered]@{
        Name = $datastore.Name
        Type = $type
        Path = $path
        FreeSpaceGB = $freeSpaceGB
        FreeSpacePercent = $freeSpacePercent
    })


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

There doesn't seem to be any code in your post.

In any case, if you want to keep the properties in the order you define them in the code, one way of doing that is to use the [ordered]@ construct

New-Object -TypeName PSObject -Property ([ordered]@{
  Prop1 = 'value'
  Prop2 = 'value'
  Prop3 = 'value'
})


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

Reply
0 Kudos
lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

Sorry missed to add the script :rolling_on_the_floor_laughing:

# Get all datastores in the vCenter Datacenters
$datastores = Get-Datastore -Location (Get-Datacenter)

# Create an empty array to hold the datastore information
$datastoreInfo = @()

# Loop through each datastore
foreach ($datastore in $datastores) {
    # Get the datastore type, host or network path, available space in GB and %
    $type = $datastore.ExtensionData.Info.GetType().Name
    $path = $datastore.ExtensionData.Info.Url
    $freeSpaceGB = "{0:N2}" -f ($datastore.FreeSpaceGB)
    if ($datastore.CapacityGB -eq 0) {
        $freeSpacePercent = "N/A"
    } else {
        $freeSpacePercent = "{0:N2}" -f ($datastore.FreeSpaceGB / $datastore.CapacityGB * 100)
    }

    # Add the information to the array
    $datastoreInfo += New-Object PSObject -Property @{
        Name = $datastore.Name
        Type = $type
        Path = $path
        FreeSpaceGB = $freeSpaceGB
        FreeSpacePercent = $freeSpacePercent
    }
}

$PreContent = "<style>
                    table {
                        border-collapse: collapse;
                        with: 100%;
                    }
                        th, td {
                                    padding: 8px;
                                    text-align: left;
                                    border-bottom: 1px solid #ddd;
                                }

                            tr:hover {
                                    background-color: coral;
                        }
                </style>"
$PostContent = "<br>"
$datastoreInfo | ConvertTo-Html -Fragment -PreContent $PreContent -PostContent $PostContent | Out-File "datastore_info.html"
$datastoreInfo | Export-Csv -Path .\datastore_info.csv -Append -NoTypeInformation -Force

# Disconnect from vCenter server
Disconnect-VIServer * -Force -Confirm:$false
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Like I said earlier, use the [ordered] construct.

    # Add the information to the array
    $datastoreInfo += New-Object PSObject -Property ([ordered]@{
        Name = $datastore.Name
        Type = $type
        Path = $path
        FreeSpaceGB = $freeSpaceGB
        FreeSpacePercent = $freeSpacePercent
    })


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