[SOLVED] Powershell function to get RAM isn't working as intended

Issue

I have written up a function that worked at one point and I have broken it while editing it.
Just a funny prank function that will get how many gigs of ram you have then roast you. Below is the full function. However it just keeps outputting the first custom response only no matter what.

function Get-BasicInfo {

# ENTER YOUR CUSTOM RESPONSES HERE
#----------------------------------------------------------------------------------------------------
    $lowRAM = "$RAM gigs of ram? thats pretty low"
    
    $okRAM = "$RAM gigs of ram really? I have a calculator with more computing power"
    
    $goodRAM = "$RAM gigs of ram? That is not terrible I guess"

    $impressiveRAM = "$RAM gigs of ram? are you serious? a super computer with no security that is funny right there"
#----------------------------------------------------------------------------------------------------

    try {

    $OS = (Get-WmiObject Win32_OperatingSystem).Name;$OSpos = $OS.IndexOf("|");$OS = $OS.Substring(0, $OSpos)

    $RAM=Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | % { "{0:N1}" -f ($_.sum / 1GB)}
    $RAMpos = $RAM.IndexOf('.')
    $RAM = $RAM.Substring(0,$RAMpos).Trim()

    if($RAM -le 4){
       return $lowRAM
    } elseif($RAM -ge 5 -and $RAM -le 12){
       return $okRAM
    } elseif($RAM -ge 13 -and $RAM -le 24){
       return $goodRAM
    } else {
       return $impressiveRAM
    }

    }
 
 # If one of the above parameters is not detected function will return $null to avoid sapi speak

    # Write Error is just for troubleshooting 
    catch {Write-Error "Error in search" 
    return $null
    -ErrorAction SilentlyContinue
    }
}

I tried to run the code in snippets to trouble shoot it and something funny happened. First I ran this just to set the response variables

    $lowRAM = "$RAM gigs of ram? thats pretty low"
    
    $okRAM = "$RAM gigs of ram really? I have a calculator with more computing power"
    
    $goodRAM = "$RAM gigs of ram? That is not terrible I guess"

    $impressiveRAM = "$RAM gigs of ram? are you serious? a super computer with no security that is funny right there"

then I ran this to get the amount of ram and set it to a variable

    $RAM=Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | % { "{0:N1}" -f ($_.sum / 1GB)}
    $RAMpos = $RAM.IndexOf('.')
    $RAM = $RAM.Substring(0,$RAMpos).Trim()

Ill test the variable $RAM and it returns 16 no problem
and finally tried to execute the if, elseif, else statements to get the response

    if($RAM -le 4){
       return $lowRAM
    } elseif($RAM -ge 5 -and $RAM -le 12){
       return $okRAM
    } elseif($RAM -ge 13 -and $RAM -le 24){
       return $goodRAM
    } else {
       return $impressiveRAM
    }

for some reason this will always just output the first custom "low ram" response.

"$RAM gigs of ram? thats pretty low"

$RAM returns 16 on my pc but doesn’t adhere to the logic above when outputting the response.

However if I just hardcode the $RAM variable myself it works…

"$RAM gigs of ram? That is not terrible I guess"

    $RAM = 16

    if($RAM -le 4){
       return $lowRAM
    } elseif($RAM -ge 5 -and $RAM -le 12){
       return $okRAM
    } elseif($RAM -ge 13 -and $RAM -le 24){
       return $goodRAM
    } else {
       return $impressiveRAM
    }

Am I just losing the variable at some point? I’ve been racking my brain on this for hours now.

Solution

This happens because the if block compares two different types. When doing so, the value is on the right-hand side of the comparison can be converted to the type of the left-hand side value for comparison.

So, what’s going on is that the comparison is to see if string 16 is less than string 4. Since string comparison is lexiographical, and 1 is less than 4, the result is not what one would expect. Should both data types be integers, then it would be a test of 16 and 4. This is a very common problem about anyone that wants to sort file names encounters pretty soon.

Fix: convert the $RAM into an integer, or reverse the test.

Answered By – vonPryz

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published.