捐血一袋救人一命

2024年3月23日 星期六

Powershell 變數特性

Power shell 是一個弱型別的 Script Language

不需要宣告,變數存甚麼,就是甚麼型態

$a="123"
$a.GetType()

$a = 456
$a.GetType()

Result:

IsPublic IsSerial Name    BaseType                              
-------- -------- ----    --------                              
True     True     String  System.Object                         
True     True     Int32   System.ValueType

要注意的是,變數初始值設定的方法

$a, $b = 1, 2 ,3

Result:

$a = 1      # 數值
$b = 2, 3   # 是陣列

function abc{
    return 1,"A","C"
}

$a, $b = abc
$a.GetType()
$b.GetType()

Result:

IsPublic IsSerial Name       BaseType                              
-------- -------- ----       --------                              
True     True     Int32      System.ValueType                      
True     True     Object[]   System.Array 

一個變數在沒有使用之前,變數等於 $null

Clear-Host
Remove-Variable * -ErrorAction SilentlyContinue

$var -eq $null
$var = 3
$var -eq $null
Clear-Variable var
$var -eq $null

Result:

True
False
True

0 意見: