基本構文(10)
MSHの変数型について
MSHの変数型は大きく分けて2種類あります。
- MSHで定義される固有の型
- 全ての.NETの型
「MSHで定義される固有の型」には以下があります。
array, bool, byte, char, char[], decimal double, float, int, int[], long, long[] regex, single, scriptblock, string, type, xml
例えば、以下のように利用します。
MSH> $a=[int]4 MSH> $a=[xml]"<test><a>avalue</a></test>"
.NETの型も同様です。
MSH> $d = [System.DateTime] "9/12/2005" MSH> $d.Month 9 MSH> $d.Day 12 MSH> $d.Year 2005 MSH> $d 2005年9月12日 0:00:00
.NETの型の場合、以下のように「System.」は省略可能です。
MSH> $d = [DateTime] "9/12/2005"
補足ですが、型を宣言した後に不適切な値を代入すると当然エラーになります。
MSH> [int]$a=4 MSH> $a = "string" : Cannot convert "string" to "System.Int32". Error: "string は Int32 の有効な値ではありません。". At line:1 char:3 + $a <<<< = "string"