存储返回值到变量



我有下面的代码来转换分配GB/TB基于大小的值

$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];

但是当我试图将返回值分配给如下变量时,我得到错误

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

请告诉我如何在变量

中存储值

为什么不创建一个小的辅助工具函数呢:

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]
}

那么获取格式化的大小就像:

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

最新更新