Home > Enterprise >  Store return value to variable
Store return value to variable

Time:10-21

I have the below code to convert assign GB/TB value based on size

$datastoreCapacity = $store.CapacityGB
$postfixes = @("GB", "TB", "PB" )
for ($i=0; $datastoreCapacity -ge 1024 -and $i -lt $postfixes.Length; $i  ) { $datastoreCapacity = $datastoreCapacity / 1024; }
return ""   [System.Math]::Round($datastoreCapacity,2)   " "   $postfixes[$i];

$datastoreFree = $store.FreeSpaceGB
$postfixes = @("GB", "TB", "PB" )
for ($i=0; $datastoreFree -ge 1024 -and $i -lt $postfixes.Length; $i  ) { $datastoreFree = $datastoreFree / 1024; }
return ""   [System.Math]::Round($datastoreFree,2)   " "   $postfixes[$i];


But when I am trying to assign the return value to variable like below i am getting error

$datastoreCapacity = return ""   [System.Math]::Round($datastoreCapacity,2)   " "   

Please let me know how can i store the value in variable

CodePudding user response:

Why not make a small helper utility function for this:

function Format-Capacity ([double]$SizeInGB) {
    $units = 'GB', 'TB', 'PB'
    for ($i = 0; $SizeInGB -ge 1024 -and $i -lt $units.Count; $i  ) {
        $SizeInGB /= 1024
    }
    '{0:N2} {1}' -f $SizeInGB, $units[$i]
}

Then getting the formatted sized is as easy as:

$datastoreCapacity = Format-Capacity $store.CapacityGB
$datastoreFree = Format-Capacity $store.FreeSpaceGB
  • Related