22
PowerShell – Object 篇 2013/11/18 Howard

Power shell – object 篇

Embed Size (px)

Citation preview

Page 1: Power shell – object 篇

PowerShell – Object 篇2013/11/18 Howard

Page 2: Power shell – object 篇

GetType• 可使用 GetType() 方法取得物件類型

Page 3: Power shell – object 篇

Array• Element 用「 , 」分隔,建立 object array

Page 4: Power shell – object 篇

如何建立 empty array• @()

• 建立 array 的方式,也可以用 @(………) • Element 用「 , 」分隔

• 利用數值區間來建立陣列• (startIndex..endIndex)

Page 5: Power shell – object 篇

取得 array 資料• 可以用 [index], [StartIndex..EndIndex] 取得資料

• 以上例, $array[3] 的輸出是什麼

• 以上例, $array[0,-1] 的輸出是什麼

Page 6: Power shell – object 篇

Array method• -contains• -notcontains• 不區分大小寫

Page 7: Power shell – object 篇

Array method• 字串查詢時,注意型態轉換

• 使用迴圈或 Where-Object 避免型別問題

Page 8: Power shell – object 篇

Dictionary• System.Collections.Hashtable

• 存取字典方式:

黃承豪
Page 9: Power shell – object 篇

Dictionary method

• 移除元素 Remove()

• Contains(), ContainsKey() 查找是否有存在給定的 Key• ContainsValue 查找是否有存在的 Value

• Keys, Values 列舉資料

Page 10: Power shell – object 篇

值比較• -eq

• equal• -ne

• Not-equal• -lt

• Less-than• -gt

• Greater-than• -le

• Less-than-or-equal• -ge

• Greater-than-or-equal

Page 11: Power shell – object 篇

值比較• 字串比較區分大小寫

• -ceq, -cne, -clt, -cle, -cgt, -cge

Page 12: Power shell – object 篇

Foreach-Object

• 與 foreach 同名

Page 13: Power shell – object 篇

$_

• 指的是當前物件的索引• 如同 JavaScript 中的 this

Page 14: Power shell – object 篇

Where-Object

• 與 where, ? 同名• 過濾並反回符合條件的項目 • 同 SQL 的 where

Page 15: Power shell – object 篇

Select-Object• 簡寫為 select• 用來新建包含原始物件屬性子集的物件集合• 同 SQL 的 select

Page 16: Power shell – object 篇

Sort-Object

• 簡寫為 sort• 可按照一個或多個屬性排序集合

Page 17: Power shell – object 篇

Tee-Object

• 簡稱 tee• 把集合傳遞給下一個命令前,保存當前管道到檔案或變數• 可在執行特定階段保存集合,並在後面的命令中使用這個集合

Page 18: Power shell – object 篇

Group-Object

• 簡稱 group• 將物件集合按照屬性值分為多個 Group

Page 19: Power shell – object 篇

Measure-Object

• 計集集合的統計值• 取得最小值、最大值、平均值等資訊

Page 20: Power shell – object 篇

Compare-Object

• 簡稱 diff• 比較兩個物件或集合並回傳不同的資訊

Page 21: Power shell – object 篇

Q1

• 列出你 pc 中的 service 列表• get-service• 其中狀態是「執行中」且 名稱是「 Win 開頭」,再按照「名稱」降幂排序。

Page 22: Power shell – object 篇

Reference