mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
Add Conversion for both IEC and SI Byte-Suffix (10^X & 2^X)
This commit is contained in:
parent
227407186b
commit
6095f6a586
3 changed files with 157 additions and 4 deletions
|
|
@ -5,19 +5,29 @@ function Convert-Bytes()
|
|||
[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)")) {
|
||||
If (($Value -Match "(^[0-9]*) ?(B|KB|MB|GB|TB|PT|Kibi|Mibi|Gibi|Tibi|Pibi)")) {
|
||||
[single]$CurrentValue = $Matches[1];
|
||||
[string]$CurrentUnit = $Matches[2];
|
||||
|
||||
$CurrentValue = ConvertTo-Byte $CurrentValue $CurrentUnit;
|
||||
switch ($CurrentUnit) {
|
||||
{ 'KiBi', 'Mibi', 'Gibi', 'Tibi', 'Pibi' -contains $_} { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true;}
|
||||
{ 'KB', 'MB', 'GB', 'TB', 'PB' -contains $_} { $CurrentValue = ConvertTo-ByteSI $CurrentValue $CurrentUnit; $boolOption = $true;}
|
||||
}
|
||||
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B' -contains $_} { $FinalValue = ConvertTo-Byte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
#{ 'B' -contains $_} { $FinalValue = ConvertTo-ByteSI $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;}
|
||||
{ 'Kibi' -contains $_} { $FinalValue = ConvertTo-KibiByte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
{ 'Mibi' -contains $_} { $FinalValue = ConvertTo-MibiByte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
{ 'Gibi' -contains $_} { $FinalValue = ConvertTo-GibiByte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
{ 'Tibi' -contains $_} { $FinalValue = ConvertTo-TibiByte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
{ 'Piti' -contains $_} { $FinalValue = ConvertTo-PetaByte $CurrentValue -Unit B; $boolOption = $true;}
|
||||
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
|
|
|
|||
143
lib/core/tools/ConvertTo-ByteUnitIEC.psm1
Normal file
143
lib/core/tools/ConvertTo-ByteUnitIEC.psm1
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
function ConvertTo-ByteIEC()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 40)); $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 50)); $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ConvertTo-KibiByte()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 40)); $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ConvertTo-MibiByte()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ConvertTo-GibiByte()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ConvertTo-TibiByte()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 40)); $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = ($Value * [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function ConvertTo-PitiByte()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
[string]$Unit
|
||||
);
|
||||
|
||||
switch ($Unit) {
|
||||
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(2, 50)); $boolOption = $true; }
|
||||
{ 'Kibi', 'KibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 40)); $boolOption = $true; }
|
||||
{ 'Mibi', 'MibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 30)); $boolOption = $true; }
|
||||
{ 'Gibi', 'GibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 20)); $boolOption = $true; }
|
||||
{ 'Tibi', 'TibiByte' -contains $_ } { $result = ($Value / [math]::Pow(2, 10)); $boolOption = $true; }
|
||||
{ 'Piti', 'PitiByte' -contains $_ } { $result = $Value; $boolOption = $true; }
|
||||
default {
|
||||
if (-Not $boolOption) {
|
||||
Throw 'Invalid input';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
.NOTES
|
||||
#>
|
||||
|
||||
function ConvertTo-Byte()
|
||||
function ConvertTo-ByteSI()
|
||||
{
|
||||
param(
|
||||
[single]$Value,
|
||||
Loading…
Reference in a new issue