Pages - Menu

2019年6月4日 星期二

[PowerShell]初學筆記-Part I

雖然寫過一些powershell的檔案
但從來沒有從頭去看過相關的書

1.PowerShell的指令是根據 動詞-名詞 這樣去湊出來的
2.一般我們在DOS下使用的DIR 或是 CD 這些指令,在PowerShell底下是靠alias去對應的

Get-Alias 可以看到所有的對應表
Get-Alias DIR 可以看到 這個指令的別名

3.指令後面可以再加上另一個命令(cmdlet) ,中間用 破折號做分隔

Get-Command | Measure-Object  可以計算命令的加總數量

4.如果要找關於service的指令,可使用

Get-Command *service* 尋找

5.更新 指令的範例(記得用Administrator更新,但我還是更新失敗)
Update-Help

6.參數的簡易介紹

Get-EventLog
    [-LogName] <string>
    [[-InstanceId] <long[]>]
    [-ComputerName <string[]>]
    [-Newest <int>][-After <datetime>]
    [-Before <datetime>]
    [-UserName <string[]>]
    [-Index <int[]>][-EntryType <string[]>]
    [-Source <string[]>]
    [-Message <string>]
    [-AsBaseObject][<CommonParameters>]
[-LogName] <string> 為必選,因沒有用中括號 [  ]   將 指令及參數包起來
[-ComputerName <string[]>] 為可選,因為使用了 [  ]   將指令及參數包起來
  [[-InstanceId] <long[]>] 同上,為可選,但InstanceId 又用了中括號包起來,表示此參數又為可選,
又稱定位參數,在輸入時可以省略-InstanceId 不用打,直接輸入參數,但須注意參數的位置

<String []>  用單引號及逗號 串連,如果是數字就不需單引號
Get-EventLog -ComputerName 'SERVER01','SERVER02'
結論
Form Meaning
[[-Param] T] Optional parameter of type T with the name optional
[-Param T] Optional parameter of type T with the name required
-Param T Required parameter of type T
[-Param] A switch (flag)
7.小括號()會最優先執行
Get-EventLog Application -computer (Get-Content names.txt)

沒有留言:

張貼留言