scage007
scage007

Reputation: 3

Cannot convert value to type "System.Int32"

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 

Upvotes: 0

Views: 9061

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

$_.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

Upvotes: 3

Related Questions