int[]配列をforeach-objectしたら結果が複数ならObject[] 、結果が1つだとint32
int[] にキャスト
PS> $arr = ([int[]](1..3)) PS> $arr.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32[] System.Array
foreach-objectするとObject[]
PS> $arr2 = $arr | %{ $_ } PS> $arr2.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
結果が1つだとint32
PS> $i = $arr | %{ if($_ -eq 2) { $_ } } PS> $i.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32 System.ValueType
Object[] をint[] にキャスト
PS m0014525> $arr3 = ([int[]]$arr2) PS m0014525> $arr3.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Int32[] System.Array
1つだけ文字列にして意地悪すると、失敗のエラー
PS m0014525> $arr2[2]="abc" PS m0014525> ([int[]]$arr2) 値 "abc" を型 "System.Int32" に変換できません。エラー: "入力文字列の形式が正しくありません。" 発生場所 行:1 文字:1 + ([int[]]$arr2) + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) []、RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger