none
How to compare integer literals with another int value? RRS feed

  • שאלה

  • I want to compare a size with a threshold that will be input by the user or be set by default if no input. the threshold should be in GB, although, user shouldnt have to enter unit.

    here is the code i have

    param($threshold)
    if(!$threshold){$threshold = 20} #threshold is GB
    write-output "$threshold GB"  
    
    Import-Module SqlServer
    
    $Analysis_Server = New-Object Microsoft.AnalysisServices.Server  
    $Analysis_Server.connect("$server")
    
    Size = $Analysis_Server.Databases[$cube].EstimatedSize
    
    if ($size -ge 1GB)
    {
        $newSize = [pscustomobject]@{
            Size = $size/1GB; Unit = 'GB'
        }
    }
    elseif ($size -ge 1MB)
    {
        $newSize = [pscustomobject]@{
            Size = $size/1MB; Unit = 'MB'
        }
    }
    elseif ($size -ge 1KB)
    {
        $newSize = [pscustomobject]@{
            Size = $size/1KB; Unit = 'KB'
        }
    }
    else
    {
        $newSize = [pscustomobject]@{
            Size = $size; Unit = 'B'
        }
    }
    if($newSize.Size -gt $threshold) {"exceeded the threshold!"}

    the last part if($newSize.Size -gt $threshold) {"exceeded the threshold!"} doesnt work because the unit is not specified. if i use integer literal for testing purposes, i am getting the following strange output of the threshold print if i set the threshold as an integer literal like this: 20GB: 21474836480


    שבת 17 אוגוסט 2019 00:55

כל התגובות

  • There's no $ before the "Size = $Analysis...". How is this working at all?

    Is the $threshold 20GB or "20 whatever" if $threshold isn't already set?

    Wouldn't it make more sense to test the $size against $threshold before you create the PSCustomObject?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    שבת 17 אוגוסט 2019 02:55
  • The problem with the example below is that if the threshold value is exceeded the result is a string instead of a PSCustomObject.

    param($threshold)
    if(!$threshold){
        $threshold = 20GB #threshold is GB
    }
    write-host ("{0: ###,###,###,###,### Bytes}" -f $threshold)  
    
    Import-Module SqlServer
    
    $Analysis_Server = New-Object Microsoft.AnalysisServices.Server  
    $Analysis_Server.connect("$server")
    
    $Size = $Analysis_Server.Databases[$cube].EstimatedSize
    
    if($Size -gt $threshold) {
        "exceeded the threshold!"
    }
    else
    {
        switch ($size){
            {$PSItem -ge 1GB} 
                {
                    $newSize = [pscustomobject]@{
                                    Size = $size/1GB; Unit = 'GB'
                                }
                    break
                }
        
            {$PSItem -ge 1MB}
                {
                    $newSize = [pscustomobject]@{
                                    Size = $size/1MB; Unit = 'MB'
                                }
                    break
                }
    
            {$PSItem -ge 1KB}
                {
                    $newSize = [pscustomobject]@{
                                    Size = $size/1KB; Unit = 'KB'
                                }
                    break
                }
            Default
                {
                    $newSize = [pscustomobject]@{
                                    Size = $size; Unit = 'B'
                                }
                    break
                }
        }
        Write-Output $newSize
        
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    שבת 17 אוגוסט 2019 19:57
  • None of this makes any sense.

    You can directly compare numbers like 20Gb because that is a number.  THe is no need to convert anything. 

    IF you want to specify that a user enters a value as an INT that represents the number of GB jus tdo this:

    $threshold = 20 * 1Gb

    If you want the user to specify any number in any format then just use the number entered.

    $threshold = 20 * 1Gb
    214Mb -gt $threshold # retuens $false
    21Gb -gt $threshold # returns $true

    No need to check Unit or create strings 214Mb is not a string - it is a number.

    $threshold = "214Gb" # is a string
    $threshold = 214G  # is a number


    \_(ツ)_/


    שבת 17 אוגוסט 2019 21:15
    מנחה דיון
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    יום שישי 06 ספטמבר 2019 12:10
    מנחה דיון