From 00f3be316380634055a98dc841a4485da649664b Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Tue, 29 Oct 2019 16:08:27 +0100 Subject: [PATCH] Generic Conversion for Bytes --- lib/core/tools/Convert-Bytes.psm1 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/core/tools/Convert-Bytes.psm1 diff --git a/lib/core/tools/Convert-Bytes.psm1 b/lib/core/tools/Convert-Bytes.psm1 new file mode 100644 index 0000000..445b1c4 --- /dev/null +++ b/lib/core/tools/Convert-Bytes.psm1 @@ -0,0 +1,30 @@ +function Convert-Bytes() +{ + param( + [string]$Value, + [string]$Unit + ); + + If (($Value -Match "(^[0-9]*) ?(B|b|kb|KB|kB|Kb|mb|Mb|mB|MB|Gb|gB|gb|GB|tb|Tb|tB|TB|PT|pt|pT|Pt)")) { + [single]$CurrentValue = $Matches[1]; + [string]$CurrentUnit = $Matches[2]; + + $CurrentValue = ConvertTo-Byte $CurrentValue $CurrentUnit; + + switch ($Unit) { + { 'B' -contains $_} { $FinalValue = ConvertTo-Byte $CurrentValue -Unit B; $boolOption = $true;} + { 'KB' -contains $_} { $FinalValue = ConvertTo-KiloByte $CurrentValue -Unit B; $boolOption = $true;} + { 'MB' -contains $_} { $FinalValue = ConvertTo-MegaByte $CurrentValue -Unit B; $boolOption = $true;} + { 'GB' -contains $_} { $FinalValue = ConvertTo-GigaByte $CurrentValue -Unit B; $boolOption = $true;} + { 'TB' -contains $_} { $FinalValue = ConvertTo-TeraByte $CurrentValue -Unit B; $boolOption = $true;} + { 'PT' -contains $_} { $FinalValue = ConvertTo-PetaByte $CurrentValue -Unit B; $boolOption = $true;} + default { + if (-Not $boolOption) { + Throw 'Invalid input'; + } + } + } + return $FinalValue; + } + Throw 'Invalid input'; +} \ No newline at end of file