2019-10-29 11:08:27 -04:00
|
|
|
function Convert-Bytes()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
[string]$Value,
|
|
|
|
|
[string]$Unit
|
|
|
|
|
);
|
|
|
|
|
|
2020-02-03 12:03:40 -05:00
|
|
|
If (($Value -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)")) {
|
2019-10-29 11:08:27 -04:00
|
|
|
[single]$CurrentValue = $Matches[1];
|
|
|
|
|
[string]$CurrentUnit = $Matches[2];
|
|
|
|
|
|
2019-10-30 08:52:37 -04:00
|
|
|
switch ($CurrentUnit) {
|
2020-02-03 12:03:40 -05:00
|
|
|
{ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB' -contains $_} { $CurrentValue = ConvertTo-ByteIEC $CurrentValue $CurrentUnit; $boolOption = $true;}
|
2019-10-30 08:52:37 -04:00
|
|
|
{ 'KB', 'MB', 'GB', 'TB', 'PB' -contains $_} { $CurrentValue = ConvertTo-ByteSI $CurrentValue $CurrentUnit; $boolOption = $true;}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 11:08:27 -04:00
|
|
|
|
|
|
|
|
switch ($Unit) {
|
2019-10-30 09:44:44 -04:00
|
|
|
{ 'B' -contains $_} { $FinalValue = $CurrentValue; $boolOption = $true;}
|
2019-10-29 11:08:27 -04:00
|
|
|
{ '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;}
|
2020-02-03 12:03:40 -05:00
|
|
|
{ 'KiB' -contains $_} { $FinalValue = ConvertTo-KiBByte $CurrentValue -Unit B; $boolOption = $true;}
|
|
|
|
|
{ 'MiB' -contains $_} { $FinalValue = ConvertTo-MiBByte $CurrentValue -Unit B; $boolOption = $true;}
|
|
|
|
|
{ 'GiB' -contains $_} { $FinalValue = ConvertTo-GiBByte $CurrentValue -Unit B; $boolOption = $true;}
|
|
|
|
|
{ 'TiB' -contains $_} { $FinalValue = ConvertTo-TiBByte $CurrentValue -Unit B; $boolOption = $true;}
|
|
|
|
|
{ 'PiB' -contains $_} { $FinalValue = ConvertTo-PetaByte $CurrentValue -Unit B; $boolOption = $true;}
|
2019-10-30 08:52:37 -04:00
|
|
|
|
2019-10-29 11:08:27 -04:00
|
|
|
default {
|
|
|
|
|
if (-Not $boolOption) {
|
2020-02-03 11:56:34 -05:00
|
|
|
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
|
2019-10-29 11:08:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-31 04:43:13 -04:00
|
|
|
return @{'value' = $FinalValue; 'pastunit' = $CurrentUnit; 'endunit' = $Unit};
|
2019-10-29 11:08:27 -04:00
|
|
|
}
|
2020-02-03 11:56:34 -05:00
|
|
|
|
|
|
|
|
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
|
2019-10-29 11:08:27 -04:00
|
|
|
}
|