Less than not evaluated correctly

Welcome Forums General PowerShell Q&A Less than not evaluated correctly

This topic contains 4 replies, has 3 voices, and was last updated by

4 years, 3 months ago.

  • Author
  • #20006

    Points: 0
    Rank: Member

    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"

  • #20009

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    TotalFreeMemPercR Is a string, for starters.

  • #20010

    Points: 2
    Rank: Member

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

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

      Points: 0
      Rank: Member

      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

    Points: 2
    Rank: Member

    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

The topic ‘Less than not evaluated correctly’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort