icinga-powershell-framework/lib/core/tools/ConvertTo-ByteUnitSI.psm1

240 lines
7.9 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Converts unit sizes to byte.
.DESCRIPTION
This module converts a given unit size to byte.
e.g Kilobyte to Byte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-Byte -Unit TB 200
200000000000000
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
function ConvertTo-ByteSI()
2019-07-17 03:42:35 -04:00
{
param(
[single]$Value,
[string]$Unit
2019-07-17 03:42:35 -04:00
);
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 12)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 15)); $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
2019-07-17 03:42:35 -04:00
return $result;
}
2019-07-23 03:34:15 -04:00
<#
.SYNOPSIS
Converts unit sizes to kilobyte.
.DESCRIPTION
This module converts a given unit size to kilobyte.
e.g byte to kilobyte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-KiloByte -Unit TB 200
200000000000
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-17 03:42:35 -04:00
function ConvertTo-KiloByte()
{
param(
[single]$Value,
[string]$Unit
);
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 12)); $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
return $result;
}
<#
.SYNOPSIS
Converts unit sizes to megabyte.
.DESCRIPTION
This module converts a given unit size to megabyte.
e.g byte to megabyte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-KiloByte -Unit TB 200
200000000
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-17 03:42:35 -04:00
function ConvertTo-MegaByte()
{
param(
[single]$Value,
[string]$Unit
);
2019-07-23 03:16:43 -04:00
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 9)); $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
return $result;
}
<#
.SYNOPSIS
Converts unit sizes to gigabyte.
.DESCRIPTION
This module converts a given unit size to gigabyte.
e.g byte to gigabyte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-GigaByte -Unit TB 200
200000
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-17 03:42:35 -04:00
function ConvertTo-GigaByte()
{
param(
[single]$Value,
[string]$Unit
);
2019-07-23 03:16:43 -04:00
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 6)); $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
return $result;
}
<#
.SYNOPSIS
Converts unit sizes to terabyte.
.DESCRIPTION
This module converts a given unit size to terabyte.
e.g byte to terabyte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-TeraByte -Unit GB 2000000
2000
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-17 03:42:35 -04:00
function ConvertTo-TeraByte()
{
param(
[single]$Value,
[string]$Unit
);
2019-07-23 03:16:43 -04:00
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = $Value; $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = ($Value * [math]::Pow(10, 3)); $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
return $result;
}
<#
.SYNOPSIS
Converts unit sizes to petabyte.
.DESCRIPTION
This module converts a given unit size to petabyte.
e.g byte to petabyte.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.EXAMPLE
PS> ConvertTo-PetaByte -Unit GB 2000000
2
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-17 03:42:35 -04:00
function ConvertTo-PetaByte()
{
param(
[single]$Value,
[string]$Unit
);
2019-07-23 03:16:43 -04:00
switch ($Unit) {
{ 'B', 'Byte' -contains $_ } { $result = ($Value / [math]::Pow(10, 15)); $boolOption = $true; }
{ 'KB', 'KiloByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 12)); $boolOption = $true; }
{ 'MB', 'MegaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 9)); $boolOption = $true; }
{ 'GB', 'GigaByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 6)); $boolOption = $true; }
{ 'TB', 'TeraByte' -contains $_ } { $result = ($Value / [math]::Pow(10, 3)); $boolOption = $true; }
{ 'PT', 'PetaByte' -contains $_ } { $result = $Value; $boolOption = $true; }
default {
2019-07-23 03:34:15 -04:00
if (-Not $boolOption) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.ConversionUnitMissing -Force;
2019-07-23 03:34:15 -04:00
}
2019-07-17 03:42:35 -04:00
}
}
return $result;
2019-07-23 03:34:15 -04:00
}