Less than not evaluated correctly

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of ReMarkIT ReMarkIT 2 years, 1 month ago.

  • Author
    Posts
  • #20006
    Profile photo of ReMarkIT
    ReMarkIT
    Participant

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

    [Double]
    $warning_freemem = 10.0,
    [Double]
    $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"

  • #20009
    Profile photo of Don Jones
    Don Jones
    Keymaster

    TotalFreeMemPercR Is a string, for starters.

  • #20010
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

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

    elseif ($TotalFreeMemPerc -lt $critical_freemem) {
     $ExitStatus = "2"
     }
    • #20040
      Profile photo of ReMarkIT
      ReMarkIT
      Participant

      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!

  • #20015
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    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
    {
       [CmdletBinding()]
     
       Param
       (
          [Parameter(ValueFromPipelineByPropertyName = $true, ValueFromPipeline=$true)]
          [String]
          $ComputerName,
          $WarningLevel = 10,
          $CriticalLevel = 5
       )
     	
       Process
       {
          $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'}
          
          [pscustomobject]@{
             Computername = $OS.CSName
             FreePhysicalMemory = [double]('{0:N2}' -f ($OS.FreePhysicalMemory / 1mb))
             TotalVisibleMemorySize = [double]('{0:N2}' -f ($OS.TotalVisibleMemorySize / 1mb))
             Status = $Status
          }
       }
    }

You must be logged in to reply to this topic.