Less than not evaluated correctly

Can anyone tell me why the following code does not evaluate correctly:

         $warning_freemem = 10.0,
         $critical_freemem = 5.0

         $ExitStatus = "0"

        $ComputerSystem = Get-WmiObject -ComputerName $ipaddress -Class Win32_operatingsystem -Property CSName, TotalVisibleMemorySize, FreePhysicalMemory
        $MachineName = $ComputerSystem.CSName
        $FreePhysicalMemory = ($ComputerSystem.FreePhysicalMemory) / (1mb)
        $TotalVisibleMemorySize = ($ComputerSystem.TotalVisibleMemorySize) / (1mb)
        $TotalVisibleMemorySizeR = “{0:N2}” -f $TotalVisibleMemorySize
        $TotalFreeMemPerc = ($FreePhysicalMemory/$TotalVisibleMemorySize)*100
        $TotalFreeMemPercR = “{0:N2}” -f $TotalFreeMemPerc
        if ($TotalFreeMemPercR -lt $warning_freemem) {
                                                    $ExitStatus = "1"
            elseif ($TotalFreeMemPercR -lt $critical_freemem) {
                                                    $ExitStatus = "2"

When i run it, i get:

Name: WK-2ZVPF5J
RAM: 7.89 GB
Free Physical Memory: 28.26 %

But ExitStatus is always equal to “2”, whereas it should be “0”

TotalFreeMemPercR Is a string, for starters.

$TotalFreeMemPercR is a string so you are doing a string comparison instead of number comparison. Try

elseif ($TotalFreeMemPerc -lt $critical_freemem) {
 $ExitStatus = "2"

You should probably also look into parameterizing this or even making it a function. I am guessing you are approaching this to report or alert on memory status for multiple machines, right? here is a quick attempt at making a function out of this so you can output objects:

function Get-MemoryStatus
      [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline=$true)]
      $WarningLevel = 10,
      $CriticalLevel = 5
      $WmiParams = @{
         Class = 'Win32_OperatingSystem'
         Property = 'CSName','TotalVisibleMemorySize','FreePhysicalMemory'
      if ($ComputerName) {$WmiParams.Add('Computername',$ComputerName)}
      $OS = Get-WmiObject @WmiParams

      $Status = 'Ok'

      if ($TotalFreeMemPercR -lt $WarningLevel) {$Status = 'Warning'}
      elseif ($TotalFreeMemPercR -lt $CriticalLevel) {$Status = 'Critical'}
         Computername = $OS.CSName
         FreePhysicalMemory = [double]('{0:N2}' -f ($OS.FreePhysicalMemory / 1mb))
         TotalVisibleMemorySize = [double]('{0:N2}' -f ($OS.TotalVisibleMemorySize / 1mb))
         Status = $Status

Hi Don,

Thanks for the starters and what about the main course? Seriously your comment seems to imply there is a lot wrong with my script so if you have further ideas for improvement, i am here to learn so please shoot!