powershell - 将返回值存储到变量
问题描述
我有以下代码可以根据大小转换分配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
推荐阅读
- android - 离子:phonegap-push-plugin 冲突
- asp.net-mvc - 作为 User.IsInRole() 评估授权角色的正确方法总是返回 false
- java - glsl片段着色器矩形不会显示
- raspberry-pi3 - 树莓派 3 B+ 红灯常亮,之前工作过
- c - Swift 编译器错误:找不到“Files.h”文件
- post - 我的博客帖子缩略图在主页上看起来很模糊
- arrays - 使用并发在旋转的排序数组中查找最小值
- git - 使用 docker 将持久的 postgres 数据推送到 git
- angular - Angular 5 - 如何对 ng5-fileupload 组件中的删除文件进行验证
- javascript - 当给出相同的结构时,为什么要求的行为与导入不同