变量的以变量调用方法
1 $str='这是一个变量' 2 $str
变量的类型
通常变量的类型有很多,这里就不一一例举了,变量类型有哪些请自行百度。
如果你需要获取一个变量的类型,想知道他是什么类型可用:
$str='这是一个变量' $str.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object
定义变量类型
1 [string]$str='这是我的' 2 [int]$num=100 3 "$str$num$"
这是我的100$
整数变量类型
1 '定义短整型' 2 [int16]$num1=-[System.Math]::Pow(2,15) 3 [int16]$num2=[System.Math]::Pow(2,15)-1 4 "取值范围$num1~-$num2" 5 '定义整型' 6 [int32]$num1=-[System.Math]::Pow(2,31) 7 [int32]$num2=[System.Math]::Pow(2,31)-1 8 "取值范围$num1~-$num2" 9 '定义长整型' 10 [long]$num1=-[System.Math]::Pow(2,63) 11 [int64]$num2=9223372036854775807 12 "取值范围$num1~$num2"
定义短整型 取值范围-32768~-32767 定义整型 取值范围-2147483648~-2147483647 定义长整型 取值范围-9223372036854775808~9223372036854775807
这里的long和int64都是一个意思。
布尔类型
1 [bool]$bool=$true 2 $bool 3 [bool]$bool=$false 4 $bool
或
1 $bool=1 2 $bool 3 $bool=0 4 $bool
True False
当然,我们也可以直接使用
1 $a=$b=$c=100 2 $a 3 $b 4 $c
100 100 100
结果$a$b$c都等于100
也可以分别赋值,如
1 $a,$b,$c=1,2,3
结果$a$b$c被分别赋值为1,2,3
1 2 3
数组的应用
1 $arr=1..9 2 $arr
或
$arr=1,2,3,4,5,6,7,8,9 $arr
1 2 3 4 5 6 7 8 9
数组的调用方法
如果我要显示数组中的5那么应该这么写
1 $arr[4]
如果我要显示1-5那么可以这样写
$arr[0..4]
如果我要求只显示1、3、5
$arr[0,2,4]
多维数组的应用
[int[][]]$arr=(1..9),(100..109) $arr[0] $arr[1]
1 2 3 4 5 6 7 8 9 100 101 102 103 104 105 106 107 108 109
或
[int[]][string[]]$arr=(1..9),(100..109) $arr[0] $arr[1]
多维数组的调用也和上面介绍的一样,如你想象。
如果定义一个不确定类型的数组可使用[array]变量名
定义字典型数组(也可称之为创建字典)
1 $test=@{name='张三';age='28岁'} 2 $test['name'] 3 $test['age']
张三
28岁
1 $test=@{name='张三','李四';age='28岁','36岁'} 2 $test['name'][0]+" "+$test['age'][0] 3 $test['name'][1]+" "+$test['age'][1]
张三 28岁
李四 36岁
以上就是变量的过程与方法。