Showing same memory amount with while loop

This topic contains 4 replies, has 3 voices, and was last updated by  Biswajit 1 month ago.

  • Author
    Posts
  • #99499

    Biswajit
    Participant
    [CmdletBinding()]
    param()
    Begin
    {
    
    $Totalmemory = (Get-WmiObject -Class Win32_operatingsystem).TotalVisibleMemorySize
    #Get Live Memory Status
    $FreeMem = ((Get-Counter -Counter "\memory\available mbytes" -MaxSamples 2 -SampleInterval 2 ).CounterSamples).CookedValue
    $FinalFreemem = ((($FreeMem | Measure-Object -Minimum).Minimum)/1024).Tostring("#.###")
    $meminGB = ($Totalmemory/1024) 
    $FinalTotalmem = ($meminGB/1024).ToString("##.###")
    $TotalFreeMemPerc = ($FinalFreemem/$FinalTotalmem)*100
    }
    Process
    {
    while($true)
    {
    
    if ($TotalFreeMemPerc -le 20)
    
    {
        $TotalFreeMemPerc
        $Bindtest= 1..3 | % {(Test-NetConnection -ComputerName ("$env:COMPUTERNAME"+"."+"$env:USERDNSDOMAIN") -Port 636).TcpTestSucceeded}
        
        $Bindtest
        
    if ($Bindtest -notcontains $false)
        {
        Write-Verbose -Message "Bind Successful"
        }
    Else
        {
                        
        Write-Error "very bad"
       }}}}
        End
        {
        
        }
  • #99501

    Olaf Soyk
    Participant

    Could you please format your code as code? And what is your question?

  • #99502

    Biswajit
    Participant

    Formatted that code & Memory is not changing while running . need to capture the live memory data.

  • #99511

    Joel Sallow
    Participant

    $FreeMem is defined outside the loop, and never redefined. You are retrieving only the current value data from the sample at one point, and never accessing the counter again.

  • #99589

    Biswajit
    Participant

    Thanks. Now that is working.

You must be logged in to reply to this topic.