Powershell-括号

时间:2019-08-20 13:50:57  来源:igfitidea点击:

Powershell支持三种类型的括号。

  1. 小括号 − ()

  2. 大括号 − {}

  3. 中括号(方括号)− []

小括号

小括号用于

  1. 传递参数

  2. 包含多组指令

  3. 解决歧义

  4. 创建数组

示例

> $array = @("item1", "item2", "item3")
 
> foreach ($element in $array) { $element }
item1
item2
item3

大括号

大括号用于

  1. 包围语句

  2. 块命令

示例

$x = 10

if($x -le 20){
   write-host("This is if statement")
}

方括号

小括号用于

  1. 访问数组

  2. 访问哈希表

  3. 使用正则表达式筛选

示例

> $array = @("item1", "item2", "item3")
 
> for($i = 0; $i -lt $array.length; $i++){ $array[$i] }
item1
item2
item3
 
>Get-Process [r-s]*
 Handles    NPM(K)     PM(K)    WS(K)   VM(M)   CPU(s)     Id    ProcessName
-------    ------     -----     -----   -----   ------     --    -----------  
    320        72     27300     33764    227     3.95    4028    SCNotification 
   2298        77     57792     48712    308             2884    SearchIndexer
   ...