天天看點

Powershell(3)

Powershell

可以使用powershell管理的服務

  • share point, exchange, lync, windows azure, window server, system center, vmware, sql server, visual studio, 硬體
  • .NET Framework是一個量身為windows定制的架構, 一款API, 解決了很多問題, .NET Framework解決了C/C++的記憶體管理, 大部分的程式運作在.NET Framework上, Window Management Frame安裝的前提是安裝了.NET Framework, 在windows上可以安裝多個.NET framework, 這樣是為了相容性考慮, 多裝一點好一點, 他是一個程式設計體系, 簡單的講這就是個類庫,有很多遊戲或者軟體都調用這個類庫裡的方法
  • WinRM(預設不開啟, 是協定的實作)
  • WMF(Windows Management Framework)可以安裝在Linux上
  • windows 羨慕Linux的指令行, 但是因為Linux的設計裡面是檔案, 而windows是API, 是以采用了powershell這個平台, ps的引擎一直都是沒有變的
  • system Int 32
  • system string
  • system boolean
  • system collection hashtable

    指令

  • get-service
  • format-volume
  • 服務: bit, bits一般不怎麼用
  • update-help -UIculture zh-CN
  • set, new, add, remove等涉及到修改類的需要指定-Name
  • 如果get-help沒有更新, 也不影響卡文法
  • enable-bitlocker: 對driver加密
  • 輸出的為對象, print對象的string
  • get-member: 擷取對象成員 --> get-service | get-member --> 通過管道傳回service對象的屬性(屬性和方法, 類似于python中的dir(obj))
  • 輸出的列明為屬性名, 輸出的不一定是該對象所有的屬性, 而是一些可以輸出的屬性, 如果要全部, get-service | select-object *, 簡短的; 或者 get-service | get-member 複雜的
  • 輸出的都是一行一行的表格, 其實他們是一個system.array對象, 輸出的是該對象中的對象的str資訊, 學過java和python懂得
  • 接受管道傳來的對象是指令的參數, 通過get-help檢視paramter, 檢視是否接受管道, 還有接受管道的優先級别

指令使用案例

  1. $var = "1" --> $var.gettype().fullname
  2. [system.int32]$a = "1" --> 提供強制轉型
  3. $services = get-services --> \(services | foreach-object {\)_.name}
  4. foreach-object必須在管道後面, foreach必須另起一行
    foreach ($item in $services) {
        $item.name
        $item.start()
        $item.stop()
    }           
  5. for循環

    for ($i = 0; $i -le 100; $i++) { $i }

  6. 定義數組$arr = @('a', 'b', 'c')
  7. 數組: .count[元素個數], $arr[0]
  8. $host.ui.writedebugline('error')
  9. $error為内置的全局變量, 是一個array, 存放異常對象
  10. $env:path --> 顯示環境變量的值, $env:pathext等等
  11. write-host, write-output, write --> echo
  12. '符是Linux中\的作用
  13. $args為參數變量
  14. $erroractionperformance --> 指令錯誤的動作
  15. invokecommand -ComputerName name -ScriptBlock {...}--> 對遠端計算機執行指令, 類似于rpc, 但是在win上是WinRM, 将scriptblock内部東西推送到執行
  16. 反序列化對象沒有close和dispose等方法
  17. system.object是所有的類的父類, 有gettype和tostring方法
為了深入了解序列化與反序列化的案例

    enable-psremoting -force
    set-wsmanquickconnfig -force

    invoke-command -computername win2012 scriptblock {get-service -name alg} | get-member
    那麼對于get-member指令是在本地執行的, 而get-service -name alg是推送到遠端主機執行的, 遠端執行完畢之後将得到的對象序列化傳回給本地主機, 本地主機将該序列化對象進行反序列化轉為反序列化
對象, 此對象是沒有close(), dispose()等方法的
    invoke-command -computername win2012 scriptblock {get-service -name alg | get-member}           

服務

  • alg: application layer gateway service
  • bit, bits, winrm

遠端連接配接(在windows server 2012 r2預設已經配置好了)

  • HTTP/HTTPS
  • 偵聽器
  • 開啟服務 set-msmanquickconfig
  • 注冊偵聽器 http5985 https5986
如何開啟(powershell管理遠端, 用戶端與服務端):
    enable-psremoting -force
    set-wsmanquickconfig -force           
  • 本地信任清單

提供程式(這裡面的項看起來是檔案, 其實不是)

  • get-childitem wsman: --> 與遠端連接配接有關, 使用set-item trusthost -value 192.168.1.7 添加信任
  • cert
  • hklm
  • hkcu
  • IIS