about_Data_Sections

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

在此插入簡介。

主題

about_Data_Sections

簡短描述

說明 Data 區段,該區段可將文字字串和其他唯讀資料,與指令碼邏輯隔離。

詳細描述

專為 Windows PowerShell® 設計的指令碼可以有一或多個只包含資料的 Data 區段。您可以在任何指令碼、函式或進階函式中,包含一或多個 Data 區段。Data 區段的內容僅限於一組指定的 Windows PowerShell 指令碼語言。

透過將資料與程式碼邏輯隔開,可讓您更輕鬆地識別及管理邏輯和資料。不僅可讓您使用個別字串資源檔案來保存文字,例如錯誤訊息和「說明」字串;也可以隔離程式碼邏輯,因此有助於確保安全性並進行驗證測試。

在 Windows PowerShell 中,Data 區段可用來支援指令碼國際化。您可以使用 Data 區段,輕鬆地隔離、尋找及處理要轉譯為多種使用者介面 (UI) 語言的字串。

Data 區段是 Windows PowerShell 2.0 的功能。使用 Data 區段的指令碼如未經修訂,將無法在 Windows PowerShell 1.0 中執行。

語法

Data 區段的語法如下:

DATA [-supportedCommand <cmdlet-name>] {

            <Permitted content>
        }

Data 關鍵字為必要項,且不區分大小寫。

允許的內容僅限於下列元素:

- All Windows PowerShell operators, except -match 
           
        - If, Else, and ElseIf statements
           
- The following automatic variables: $PsCulture,  $PsUICulture,  $True,
          $False, and $Null

        - Comments

        - Pipelines

        - Statements separated by semicolons (;)

        - Literals, such as the following:

            a

            1
  
            1,2,3

            "Windows PowerShell 2.0"

            @( "red", "green", "blue" )

            @{ a = 0x1; b = "great"; c ="script" }

            [XML] @'
             <p> Hello, World </p>
            '@

        - Cmdlets that are permitted in a Data section. By default, only the 
          ConvertFrom-StringData cmdlet is permitted.

        - Cmdlets that you permit in a Data section by using the 
          SupportedCommand parameter.

當您在 Data 區段中使用 ConvertFrom-StringData Cmdlet 時,您可以使用以單引號或雙引號括住的字串,或者使用以單引號或雙引號括住的 here-string,來括住索引鍵/值組。不過,包含變數和子運算式的字串必須使用以單引號括住的字串或以單引號括住的 here-string 來括住,以防止展開變數或執行子運算式。

SUPPORTEDCOMMAND

SupportedCommand 參數可讓您表示只產生資料的 Cmdlet 或函式。其設計目的是為了讓使用者可以在 Data 區段中,包含其所撰寫或測試的 Cmdlet 和函式。

SupportedCommand 的值是一或多個 Cmdlet 或函式名稱的逗號分隔清單。

例如,下列 Data 區段包含使用者撰寫的 Format-XML Cmdlet,可格式化 XML 檔案中的資料:

  DATA -supportedCommand Format-XML 
          {    
             Format-XML -strings string1, string2, string3
          }
       

使用 DATA 區段

若要使用 Data 區段的內容,請將其指派給變數並使用變數標記法來存取內容。

例如,下列的 Data 區段會包含 ConvertFrom-StringData 命令,以將 here-string 轉換成雜湊表,再將雜湊表指派給 $TextMsgs 變數。

$TextMsgs 變數不是 Data 區段的一部分。

$TextMsgs = DATA {
              ConvertFrom-StringData -stringdata @'
                Text001 = Windows 7
                Text002 = Windows Server 2008 R2
          '@
          }

若要存取 $TextMsgs 中之雜湊表的索引鍵和值,請使用下列命令。

$TextMsgs.Text001      
          $TextMsgs.Text002

範例

簡單的資料字串。

        DATA {
            "Thank you for using my Windows PowerShell Organize.pst script."
            "It is provided free of charge to the community."
            "I appreciate your comments and feedback."
        }

包含允許變數的字串。

        DATA {
            if ($null) {
       "To get help for this cmdlet, type get-help new-dictionary."
            }
        }

使用 ConvertFrom-StringData Cmdlet 之以單引號括住的 here-string:

        DATA {
          ConvertFrom-StringData -stringdata @'
            Text001 = Windows 7
            Text002 = Windows Server 2008 R2
        '@
        }

使用 ConvertFrom-StringData Cmdlet 之以雙引號括住的 here-string:

        DATA  {
          ConvertFrom-StringData -stringdata @"
            Msg1 = To start, press any key.
            Msg2 = To exit, type "quit".
        "@
        }

包有產生資料之使用者撰寫 Cmdlet 的 Data 區段:

DATA -supportedCommand Format-XML {    
           Format-XML -strings string1, string2, string3
        }

另請參閱

about_Automatic_Variables

about_Comparison_Operators

about_Hash_Tables

about_If

about_Operators

about_Quoting_Rules

about_Script_Internationalization

ConvertFrom-StringData

Import-LocalizedData