1

suppose i have the following code :

$size = 23.9
$size = "$size GB"
write $size

i want to use the same variable for other things, i.e.

if ($size -lt 20)
{...}

this will obviously be an issue because $size here has GB/is a string

how do i ignore the string part?

i am looking for something like this:

if($($size -replace ("anything after the numbers", "")) -lt 20)
{....}
Cataster
  • 3,081
  • 5
  • 32
  • 79
  • Instead of saving the output string to a variable, just use `Write-Output "$size GB"`. Also, avoid using aliases (ie: `write`) in your scripts – spicy.dll Aug 16 '19 at 21:17
  • @MasonSchmidgall no no write is just for easy sample here. i am actually using a custom object but its not important in the context here, im trying to do something else which is why i need to replace the string portion – Cataster Aug 16 '19 at 21:19
  • 1
    I would recommend having the numeric value separate from the unit in your custom object. This will save you a lot of code down the line. – spicy.dll Aug 16 '19 at 21:26
  • @MasonSchmidgall ya, Admin's answer gave me another insight into this...i think $unit is a good way to do this – Cataster Aug 16 '19 at 21:26

1 Answers1

4

If you insist on working with a string and keeping with the -replace operator, you can use the following:

[double]($size -replace "[^\d\.]+$")

If you maintain a numeric value type like int or double, you can use other means to work with your data. You can still output a string while keeping $size a double.

$size = 23.9
$unit = 'GB'
"{0} {1}" -f $size,$unit
23.9 GB

A very similar concept as the above example would be to create $size as a custom object.

$size = [pscustomobject]@{Size = 23.9; Unit = 'GB'}
"{0} {1}" -f $size.Size,$size.Unit

You can do dynamic unit assignment. If we assume you are starting with a size in bytes, you can assign the unit and do the conversion.

if ($size -ge 1GB)
{
    $newSize = [pscustomobject]@{
        Size = $size/1GB; Unit = 'GB'
    }
}
elseif ($size -ge 1MB)
{
    $newSize = [pscustomobject]@{
        Size = $size/1MB; Unit = 'MB'
    }
}
elseif ($size -ge 1KB)
{
    $newSize = [pscustomobject]@{
        Size = $size/1KB; Unit = 'KB'
    }
}
else
{
    $newSize = [pscustomobject]@{
        Size = $size; Unit = 'B'
    }
}
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • is it possible to have the unit part in the pscustom object dynamic? isntead of hardcoded GB, could it be conditional unit, based on this answer? https://stackoverflow.com/questions/57530347/how-to-convert-value-to-kb-mb-or-gb-depending-on-digit-placeholders/57531178#57531178 – Cataster Aug 16 '19 at 21:46
  • Sure. But you need your initial unit to be consistent. I added a snippet that can do something like that. – AdminOfThings Aug 16 '19 at 22:15
  • oh i meant inside the pscutomobject...for example, [pscustomobject]@{Size = 23.9; Unit = 'KB'?'MB'?'GB'} – Cataster Aug 16 '19 at 22:33
  • Sure. I added a snippet that will do that. – AdminOfThings Aug 16 '19 at 22:42
  • is it the if statements? – Cataster Aug 16 '19 at 22:51
  • Yes. It is in the if statements. – AdminOfThings Aug 16 '19 at 23:17
  • oh now i see the changes. it looks like you adde dthe pscustomobject in the conditional statements now, for some reason i wasnt seeing them before – Cataster Aug 16 '19 at 23:19
  • question, suppose i have an update statement. how would i combine the size and unit for the size column value? i.e. update table SET [size] = '$newSize.Size $newSize.Unit' where ....this is giving me this error: Invoke-Sqlcmd : String or binary data would be truncated. The statement has been terminated. – Cataster Aug 16 '19 at 23:53