Forum Discussion

JulianMilano's avatar
JulianMilano
Copper Contributor
Jan 11, 2024

IndexOf does not return the first value of an array!?

I'm tearing my hair out to find out why my code is not returning the first element of an array and thus created this sample code to test what I was seeing and found that the same thing happens. In t...
  • JulianMilano's avatar
    Jan 11, 2024

    I figured it out. The variables have different types so IndexOf will never find the value in the array:

    $minValue | Get-Member
       TypeName: System.Double
    
    100 | Get-Member
       TypeName: System.Int32

    The solution is to convert the lookup value from a Double to Integer:

     

    [int]$minValue = ($containers | Measure-Object -Minimum).Minimum
    # or 
    $minValue = ($containers | Measure-Object -Minimum).Minimum -as [int]

     

     Tested and works gr8!

Resources