Home > Blockchain >  Cannot convert value to type "System.Int32"
Cannot convert value to type "System.Int32"

Time:11-05

I'm trying to import a csv file and select only columns where the number is above the a certain value (80):

import-csv 'C:\temp\t\xxxx.csv' | where-object {[int]$_.utilization -gt 80} 

The error I'm getting is:

Cannot convert value "17 %" to type "System.Int32". Error: "Input string was not in a correct format."
At line:57 char:22
       import-csv 'C:\temp\t\xxxx.csv' | where-object {[int]$_.utilization -gt 80}
                                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~
     CategoryInfo          : InvalidArgument: (:) [], RuntimeException
     FullyQualifiedErrorId : InvalidCastFromStringToInteger

$_utilization seems to be an Object

Any thoughts?

I tried to convert with $util = [int]$_.utilization, but I get the following error:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32".
At line:3 char:1
    $util = [int]$_.utilization
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    FullyQualifiedErrorId : ConvertToFinalInvalidCastException 

CodePudding user response:

$_.utilization contains a string. To parse the numeric prefix as an integer, remove the space and percentage sign from the string first:

[int]($_.utilization -replace ' %$') -gt 80
  • Related