locked
Same PowerShell Script with different Result RRS feed

  • Question

  • Hi Guys

    I have been trying to run below powershell command and i am getting different result from different servers.

    ----------------------------------Script section-------------------------------------------------

    $folder = "G:\WinEvt\Logs"

    $foldersize = (Get-ChildItem $folder | Measure-Object -property length -sum )
    $foldersize
    #and convert it to GB's
    $GBsize = "{0:N2}" -f ($foldersize.sum/ 1GB)

    $GBsize -gt 400

    ----------------------------------End of Script section-------------------------------------

    we have two Identical windows 2008r2 servers and has 60GB of data on the folder mention. One server displays result "FALSE" and another will display "TRUE".

    I am trying to find out why this could happen so will appreciate if any one of you could guide me through.

    Thank you

    Tuesday, October 2, 2012 2:10 AM

Answers

  • The problem is that this syntax:


    $GBsize = "{0:N2}" -f $GBsize

    Converts $GBsize into a string. To state what I hope is now obvious, strings are not numbers and as such compare differently.

    To remedy this, do not convert to a string and just compare the numbers instead.

    Bill

    • Proposed as answer by jrv Tuesday, October 2, 2012 2:40 PM
    • Marked as answer by akg1 Wednesday, October 3, 2012 12:50 AM
    Tuesday, October 2, 2012 2:36 PM

All replies

  • There is no need to convert $GBSize to a string before comparing it.

    Instead of this:


    $GBsize = "{0:N2}" -f ($foldersize.sum/ 1GB)
    $GBsize -gt 400

    You can simply write this instead:


    $GBSize -gt 400GB

    Bill

    • Proposed as answer by Bigteddy Tuesday, October 2, 2012 5:06 AM
    Tuesday, October 2, 2012 2:27 AM
  • That is correct.  The folder are different sizes.


    ¯\_(ツ)_/¯

    Tuesday, October 2, 2012 2:50 AM
  • not sure what does that mean jrv?
    Tuesday, October 2, 2012 2:53 AM
  • Hi AbqBill

    Some Reason it is still giving me same result. 

    I modified original script and it works on the problematic server but doesn't work with the server which didn't had problem first place(same script). I am very interested to know why this is happening.....

    ------------------------------------Script----------------------------

    #put our folder we want to check here
    $folder = "G:\WinEvt\Logs"
    #now we need to see how big that folder is
    $foldersize = (Get-ChildItem $folder | Measure-Object -property length -sum )
    #and convert it to GB's
    $GBsize = "{0:N2}" -f ($foldersize.sum/ 1GB)

    $sum = 400 - $GBSIZE

    #now, let's check to see if it's over 400GBs
    If ($sum -gt 400)
        #if it is, we want to DO the following
        {do
            #Let's get the 1st file (sorted by lastwrite time and remove it
            {dir $folder | sort lastwritetime | select -first 1 | remove-item -force
                #now let's recheck the folder size
                $foldersize = (Get-ChildItem $folder | Measure-Object -property length -sum )
                $GBsize = "{0:N2}" -f ($foldersize.sum/ 1GB)
                #print the folder size for testing
                $Gbsize
       $sum = 400 - $GBSIZE
            }
            #is the folder less than 400gb? Yes, we are done. No, go back and delete another file
            until ($sum -lt 400)
            Write-Host "Deletes Done"
        }
     else {"No deletes Needed"}

    --------------------------------------END--------------------------------------

    Tuesday, October 2, 2012 5:44 AM
  • You say it doesn't work but faile to say how.  What is the error?

    You are trying to compare many things that will not compare as expected.

    This is what Bill is trying to show you.

    $folder = "G:\WinEvt\Logs"
    $foldersize=Get-ChildItem $folder | Measure-Object -property length -sum
    $sum=$foldersize.sum
    If($sum -gt 400Gb){
        "Sum is greater than 400Gb [{0:N2}]" -f ($sum/1Gb)
    }else{
         "Sum is less that 400Gb [{0:N2}]" -f $($sum/1Gb)
    }

    Run that and just change the folder name


    ¯\_(ツ)_/¯

    Tuesday, October 2, 2012 5:59 AM
  • The problem is that this syntax:


    $GBsize = "{0:N2}" -f $GBsize

    Converts $GBsize into a string. To state what I hope is now obvious, strings are not numbers and as such compare differently.

    To remedy this, do not convert to a string and just compare the numbers instead.

    Bill

    • Proposed as answer by jrv Tuesday, October 2, 2012 2:40 PM
    • Marked as answer by akg1 Wednesday, October 3, 2012 12:50 AM
    Tuesday, October 2, 2012 2:36 PM
  • Thanks AbqBill

    Yes You are right and i the change script as below and now giving me consistent result.

    $folder = "G:\test\"

    #now we need to see how big that folder is
    $foldersize = (Get-ChildItem $folder | Measure-Object -property length -sum )
    $foldersize

    $foldersize.sum -gt 40GB 

    Thank you Again.

    Wednesday, October 3, 2012 12:50 AM