Windows PowerShell:建置更完善的函式

Windows PowerShell 2.0 中的進階函式可讓您以相當簡單的指令碼模擬原始 Cmdlet。

Don Jones

在 Windows PowerShell 2.0 中,Microsoft 會引入一個新類型的函式稱為 「 進階的函數 」。一大堆人員呼叫這個 「 指令碼指令程式"。使用這些函式的概念是您現在可以使用簡化的指令碼語言的 Windows PowerShell 來建立看起來,運作方式,看起來很適合,和感覺幾乎完全像真實、 原生的 Windows PowerShell 指令程式。

很明顯地,不是所有您要就定位,以建立您自己和您的同事的可重複使用的工具。 不過,如果您要建立可重複使用的工具,這些進階的功能會前往的唯一方法。 事實上,目前修改我的教室教育軟體,只教導這種函式。

因為進階的功能運作,很像真實的 cmdlet,適當地修改函式可能會讓其他人使用,因為它 「 符合 」 的方式,其餘的殼層運作更容易。 被的情況下,這裡的範本可讓您更快速地建立這些進階功能:

function Get-Something {
  <#
  .SYNOPSIS
  Describe the function here
  .DESCRIPTION
  Describe the function in more detail
  .EXAMPLE
  Give an example of how to use it
  .EXAMPLE
  Give another example of how to use it
  .PARAMETER computername
  The computer name to query. Just one.
  .PARAMETER logname
  The name of a file to write failed computer names to. Defaults to errors.txt.
  #>
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$True,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$True,
      HelpMessage='What computer name would you like to target?')]
    [Alias('host')]
    [ValidateLength(3,30)]
    [string[]]$computername,
        
    [string]$logname = 'errors.txt'
  )

  begin {
  write-verbose "Deleting $logname"
    del $logname -ErrorActionSilentlyContinue
  }

  process {

    write-verbose "Beginning process loop"

    foreach ($computer in $computername) {
      Write-Verbose "Processing $computer"
      # use $computer to target a single computer
    

      # create a hashtable with your output info
      $info = @{
        'info1'=$value1;
        'info2'=$value2;
        'info3'=$value3;
        'info4'=$value4
      }
      Write-Output (New-Object –TypenamePSObject –Prop $info)
    }
  }
}

有幾個重要的事情注意到這個函式,其中有許多需要調整時使用此範本:

  • 函式名稱應該看起來,啟動常用的 Windows PowerShell 動詞像"Get",並結束單數名詞的其中一個指令程式的名稱。
  • 填入註解為基礎的說明。 提供每個參數的描述,描述整體的函式,以及其使用方式的範例。 在這種方式撰寫說明執行說明 about_comment_based_help 如需詳細資訊。
  • 第一個參數,$ 電腦名稱,是相當複雜。 它可以接受一或多個值的參數或管線。 它只接受三個到 30 個字元的值。 執行函式時,您可以使用 –host 的 –computername 而不是。 這個參數是必要欄位。 如果有人執行函式而不需提供電腦名稱時,命令介面將會提示他們輸入一或多。
  • 第二個參數,$logname,是一個簡單的參數。 它只接受字串值。 它也會有預設值。
  • 開始指令碼區塊會嘗試刪除任何現有的記錄檔,所以每次執行此函式有最新的記錄檔。
  • 在處理程序指令碼區塊內,我已經指示您使用 $電腦變數具有您使用單一電腦的函式。 您不要直接使用 $ 電腦名稱。 該參數可以包含一或多個值,所以 ForEach 迴圈列舉它們,並只是一次進入 $電腦了。
  • 將 $資訊雜湊表應包含您想要的輸出格式。 將此視為表格式的輸出。 我建立了稱為 info1、 info2、 info3 及 info4 的資料行。 只有在提取這些資料行的值從 $valuex 變數。 這只是一個範例。 我還沒有實際放置任何內容到此範本中的這些變數,您可以取代 $value1,$value2 等等與您自己的資訊。

此範本是適用於任何進階的功能,必須取得的資訊,但實際上並不是對系統的狀態中的任何變更。 將系統狀態的進階功能,您必須實作一些額外的項目。 這一點之後下, 面是第二個範本:

function Do-Something {
  <#
  .SYNOPSIS
  Describe the function here
  .DESCRIPTION
  Describe the function in more detail
  .EXAMPLE
  Give an example of how to use it
  .EXAMPLE
  Give another example of how to use it
  .PARAMETER computername
  The computer name to query. Just one.
  .PARAMETER logname
  The name of a file to write failed computer names to. Defaults to errors.txt.
  #>
  [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  param
  (
    [Parameter(Mandatory=$True,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$True,
      HelpMessage='What computer name would you like to target?')]
    [Alias('host')]
    [ValidateLength(3,30)]
    [string[]]$computername,
        
    [string]$logname = 'errors.txt'
  )

  begin {
  write-verbose "Deleting $logname"
    del $logname -ErrorActionSilentlyContinue
  }

  process {

    write-verbose "Beginning process loop"

    foreach ($computer in $computername) {
      Write-Verbose "Processing $computer"
      if ($pscmdlet.ShouldProcess($computer)) {
        # use $computer here
      }
    }
  }
}

在此範本中,您必須修改"ConfirmImpact"設定。 您必須提供進階的功能的潛在危險的相關指示。 例如,做事次要像變更檔案的唯讀屬性可能的評分 ConfirmImpact 的 「 低 」。重新啟動電腦,可能是 ConfirmImpact 的 「 高 」。Windows PowerShell 會使用此設定,連同殼層的內建的 $ConfirmPreference 變數,來決定是否要執行的函式時,自動提供 「 您確定? 」 提示。

它已有註明,您應該實際執行您的動作,使用 $電腦為目標的單一電腦。 換行,在"If"的建構,它使用內建的 $pscmdlet 物件。 如果此函式的 – whatif] 或 [–confirm 參數 (它將支援者) 與執行,$pscmdlet 物件會負責產生 「 如果 」 輸出或產生 「 您確定? 」 提示。

這兩個這些範本也會支援 –verbose 參數,並使用寫入詳細資訊,來產生函式執行時的狀態訊息。 很明顯地,並非每個函式必須為目標電腦。 有時候,您可以接受檔名]、 [使用者名稱] 或 [其他資訊的參數。 只要適當地修改的參數宣告。

您可以找到的 [參數] 屬性的詳細資訊,例如進行驗證,強制性,管線輸入等等 — 藉由執行說明命令介面中的 about_functions_advanced_parameter。 慢慢享用吧!

Don Jones

Don Jones是較受歡迎 Windows PowerShell 作者、 訓練和喇叭。 他最新的活頁簿是"了解 Windows PowerShell 中月的書籍 」 (Manning,2011年;) 請造訪 MoreLunches.com 的資訊] 和 [可用的小幫手內容。 他也只適用於線上訓練類別 (請參閱 ConcentratedTech.com/training 如需詳細資訊)。

相關內容