How to convert value to KB, MB, or GB depending on digit placeholders?

浪子不回头ぞ 提交于 2020-01-16 13:08:10

问题


I have the following

Import-Module SqlServer

$Analysis_Server = New-Object Microsoft.AnalysisServices.Server  
$Analysis_Server.connect("$server")

$estimatedSize = $([math]::Round($($($Analysis_Server.Databases[$cube].EstimatedSize)/1024/1024),2))

this generates for me the size in MB

I would like to enhance this to make it more user friendly readable especially for values that are in the double digit MB

for example, sometimes i get values 50.9 MB which is good. but some other values are 37091 MB or 3082.86 MB, and i'd like values like that to be automatically converted to GB (37.09 GB, 3.08 GB respectively) if they are in the GB range.

and if there are values that are not in MB range, they should be displayed in KB

i.e. 0.78 MB should just be 780 KB

how can i accomplish this?


回答1:


Here's two more ways of formatting a size in bytes:

1) Using a switch()

function Format-Size() {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [double]$SizeInBytes
    )
    switch ([math]::Max($SizeInBytes, 0)) {
        {$_ -ge 1PB} {"{0:N2} PB" -f ($SizeInBytes / 1PB); break}
        {$_ -ge 1TB} {"{0:N2} TB" -f ($SizeInBytes / 1TB); break}
        {$_ -ge 1GB} {"{0:N2} GB" -f ($SizeInBytes / 1GB); break}
        {$_ -ge 1MB} {"{0:N2} MB" -f ($SizeInBytes / 1MB); break}
        {$_ -ge 1KB} {"{0:N2} KB" -f ($SizeInBytes / 1KB); break}
        default {"$SizeInBytes Bytes"}
    }
}

2) By performing a loop where the gives byte size is repeatedly divided by 1024

function Format-Size2 {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [double]$SizeInBytes
    )

    $units = "Bytes", "KB", "MB", "GB", "TB", "PB", "EB"
    $index = 0

    while ($SizeInBytes -gt 1024 -and $index -le $units.length) {
        $SizeInBytes /= 1024
        $index++
    }
    if ($index) {
        return '{0:N2} {1}' -f $SizeInBytes, $units[$index]
    }

    return "$SizeInBytes Bytes"
}



回答2:


I believe this is a simple implementation of a conditional, control, statement like IF().

Powershell Basics: IF Statements

Example Psuedo Code:

If ($estimatedSize -lt 1)  {

 $newKbEstimateSize = $estimatedSize *  1000; //MB to KB

}

If ($estimatedSize  -gt 999.9)  {

  $newGbEstimatedSize = $estimatedSize / 1000; //MB to GB

}



回答3:


I would use the following code:

Import-Module SqlServer

$Analysis_Server = New-Object Microsoft.AnalysisServices.Server  
$Analysis_Server.connect("$server")

# Get the size in bytes
$estimatedSize = $Analysis_Server.Databases[$cube].EstimatedSize

$convertFactor = 1kb

# Check if >= 1gib
if ($estimatedSize -ge 1gb) {
    # Will convert to gibibytes
    $convertFactor = 1gb
}
elseif ($estimatedSize -ge 1mb) {
    # Will convert to mibibytes
    $convertFactor = 1mb
}

# Number of bytes / Number of Bytes in unit
$estimatedSize = [math]::Round($estimatedSize / $convertFactor, 2)

The above takes the amount in bytes and compares it using the numeric multipliers for data storage. If the amount in bytes is greater, it uses that conversion. Otherwise, it just uses Kibibytes




回答4:


Leaving the KB: 1024 vs 1000 discussion aside as we should use KiB but nobody does, including Microsoft (PowerShell, Explorer, etc.):

PS C:\> 1Kb
1024

Using the The Windows Shell shlwapi.h StrFormatByteSize function:

$Shlwapi = Add-Type -MemberDefinition '
    [DllImport("Shlwapi.dll", CharSet=CharSet.Auto)]public static extern int StrFormatByteSize(long fileSize, System.Text.StringBuilder pwszBuff, int cchBuff);
' -Name "ShlwapiFunctions" -namespace ShlwapiFunctions -PassThru

Function Format-ByteSize([Long]$Size) {
    $Bytes = New-Object Text.StringBuilder 20
    $Return = $Shlwapi::StrFormatByteSize($Size, $Bytes, $Bytes.Capacity)
    If ($Return) {$Bytes.ToString()}
}

Examples:

PS C:\> Format-ByteSize 37091MB
36.2 GB
PS C:\> Format-ByteSize 3082.86MB
3.00 GB
PS C:\> Format-ByteSize 0.78MB
798 KB
PS C:\> Format-ByteSize 670
670 bytes


来源:https://stackoverflow.com/questions/59549998/get-disk-size-minus-reserved-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!