2010年8月25日 星期三

PowerShell Learning Note (1) - Function


主要參考來源 : http://technet.microsoft.com/zh-tw/library/dd125504.aspx
  • Declare : function [] [([type]$parameter1[,[type]$parameter2])]  {
              param([type]$parameter1 [,[type]$parameter2])

              dynamicparam {}
  
              begin {}
              process {}
              end {}
          }
    • : 指定function的可視範圍, 也可用於變數, 主要應用於此, 另一議題, 需再另深入探討
      private :
      local :
      script :
      global :
    • param([type]$parameter1 [,[type]$parameter2]) : 引入參數, 同在 function_name 後使用([type]$parameter1[,[type]$parameter2]), 這樣的方法較接近一般的語言 ...
    • dynamicparam 
    • 可用 return 來回傳值, 但注意: 所有會產生將結果print out 到console的statement, 都回成為回傳值的一部分
    • begin, process, end : 當使用pipeline 呼叫function時, 才會產生效果
      Begin:只會在函式一開始時執行一次,適合放置初始動作的程式碼,例如初始變數。
      Process:至少執行一次,但有可能重複執行數次。
      End:只會在函式結束前執行一次。
  • Example :
    function my_function_test ([string]$str, [int]$intValue1, [int]$intValue2) {
       $str=$intValue1 + $intValue2
       return $str
    }

2010年8月24日 星期二

PowerShell Learning Note (1) - Data Type



  • Declare : start with '$', 不分大小寫
    • example : $a=3, $b="4", 
    • [int]$value=39 ==> assign type to [int]
  • Data Type

  • [adsi]
    AD 服務物件
    [array]
    陣列
    [bool]
    布林值 (True 或 False)
    [byte]
    8 位元無號整數
    [char]
    單一個 Unicode 字元(16位元)
    [datetime]
    日期或時間
    [decimal]
    128 位元十進位值
    [double]
    倍精度 64 位元浮點數
    [hashtable]
    雜湊表物件
    [int] 或 [int32]
    32 位元有號整數
    [long]
    64 位元有號整數
    [regex]
    規則運算式
    [single]
    單精度 32 位元浮點數
    [scriptblock]
    程式區塊
    [string]
    固定長度的 Unicode 字串
    [WMI]
    WMI 執行個體或集合
    [WMIclass]
    WMI 類別
    [xml]
    Xml 物件
  • $var | get-Member ==> Variable is an object, This can use to get different type's variable's attribute and member function to know how to handle it
    • $str="a string"
      $len=$str.Length