0

In Azure DevOps pipeline, I have written a PowerShell script in variables for storage are defined, those variables will be given while running the pipeline. I need to take those variable show as GB.

  1. Here in below example x,y,z values given in pipeline like 256, 512, 1024. Those values should be considered as 256GB, 512GB, 1024GB in my PowerShell script

  2. In the same code I need to give 3 TB value to variable $Maxsize. Did I mentioned in correct format or is there any other format?

PowerShell script:

$sum = $x+$y+$z 

$Maxsize = 3 / 1TB
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

It is unclear what direction you want to go (or at least there is some confusion).

To convert from GB/TB:

Directly using the interpreter:

$Maxsize = 3TB
$Maxsize

Yields (the maximal size in bytes):

3298534883328

(note: KB: 1024 vs 1000)

From a string:

$x = '256GB'
$y = '512GB'
$z = '1024GB'

$Total = 0 + $x + $y +z

Note that the implicit type casting will follow the type of the first operand (0) and add all following variables as numbers (instead of strings).

From a integer (that represents GB units):

$x = 256
$y = 512
$z = 1024

$Total = 0 + "$($x + $y + $z)GB"

In this example the total of $x + $y + $z will be converted to a string and suffixed with GB and than implicitly converted to a number by adding it to a numeric value (0).

To convert to GB/TB:

For this you might use the Format-ByteSize function or one of the other answers to the question How to convert value to KB, MB, or GB depending on digit placeholders:

Format-ByteSize $Total

Yields:

1.75 TB
iRon
  • 20,463
  • 10
  • 53
  • 79