Software Development Kit (SDK) and API Discussions

script to addup volume used and available size

sakalava
576 Views

 

Hello All, 

 

I am attempting to create a script which will add up the total available and used size of all volume 

here is my script 

$vols = Get-NcVol
$voltotal = $vols | Measure-Object -Property TotalSize -Sum
$voltotalsize = ConvertTo-FormattedNumber $voltotal.sum DataSize -Verbose
$volused = $vols | Measure-Object -Property Used -Sum
$volusedsize = ConvertTo-FormattedNumber $volused.sum DataSize -Verbose

 

It is giving me wrong numbers I think this is because the get-ncvol give  volume size in Gb,Tb and Mb so we basically can not add apple to orange 

Has anyone everdone that ? can you give me an exemple ? 

1 ACCEPTED SOLUTION

sakalava
443 Views

thanks a lot 

 I think I found an alternative 

 

Get-NcVol | ForEach-Object {
$x = "" | Select Name,Total_TB,Available_TB
$x.Name = $_.Name
$x.Total_TB = [Math]::Round($_.TotalSize / 1TB, 2)
$x.Available_TB = [Math]::Round($_.Available / 1TB, 2)
$x | Format-Table -AutoSize | Out-String | ForEach-Object { $_.Trim("`r","`n") }

View solution in original post

2 REPLIES 2

JohnChampion
521 Views

Perhaps convert everything to bytes first then total and convert back to whichever unit you want?

 

function Convert-UnitsToBytes {
    [cmdletbinding()]
    param (
        [Parameter(Mandatory = $True)]
        [uint64]$Size,
        [Parameter(Mandatory = $True)]
	    [ValidateSet('MB', 'GB', 'TB')]$Unit
    )

    # Converts BYTES to a specified UNIT (MB|GB|TB)
    #   - Any other Unit will return the original value passed in

    $Unit = $Unit.ToUpper()

    switch ($Unit) {
        'MB' {
            $bytes = $size * 1MB
        }
        'GB' {
            $bytes = $size * 1GB
        }
        'TB' {
            $bytes = $size * 1TB
        }
        Default {
            $bytes = $size
        }
    }

    return $bytes

}

sakalava
444 Views

thanks a lot 

 I think I found an alternative 

 

Get-NcVol | ForEach-Object {
$x = "" | Select Name,Total_TB,Available_TB
$x.Name = $_.Name
$x.Total_TB = [Math]::Round($_.TotalSize / 1TB, 2)
$x.Available_TB = [Math]::Round($_.Available / 1TB, 2)
$x | Format-Table -AutoSize | Out-String | ForEach-Object { $_.Trim("`r","`n") }

Public