about_Data_Sections
適用対象: Windows PowerShell 2.0, Windows PowerShell 3.0
概要をここに挿入してください。
トピック
about_Data_Sections
概要
スクリプトのロジックからテキスト文字列やその他の読み取り専用データを分離する Data セクションについて説明します。
詳細説明
Windows PowerShell® 用に設計されたスクリプトには、データのみを含む 1 つまたは複数の Data セクションを含めることができます。任意のスクリプト、関数、または高度な関数に、1 つまたは複数の Data セクションを含めることができます。Data セクションの内容は、Windows PowerShell スクリプト言語の規定されたサブセットに制限されます。
コードのロジックからデータを分離することで、ロジックとデータの両方の識別と管理がしやすくなります。テキスト (エラー メッセージやヘルプ文字列など) の文字列リソース ファイルを別個に作成できるようになります。また、コード ロジックも分離されるため、セキュリティ テストや検証テストがしやすくなります。
Windows PowerShell では、Data セクションを使用してスクリプトの国際化がサポートされます。Data セクションを使用して、多くのユーザー インターフェイス (UI) 言語に翻訳される文字列を簡単に分離、特定、および処理できます。
Data セクションは Windows PowerShell 2.0 の機能です。Windows PowerShell 1.0 では、Data セクションを含むスクリプトは改訂しないと実行できません。
構文
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 コマンドレットを使用するときは、一重引用符または二重引用符付きの文字列または一重引用符または二重引用符付きのヒア文字列の中にキー/値ペアを入れることができます。ただし、変数や部分式を含む文字列は、変数が展開されたり、部分式が実行可能になったりしないように、一重引用符付きの文字列または一重引用符付きのヒア文字列の中に入れる必要があります。
SupportedCommand
SupportedCommand パラメーターを使用すると、コマンドレットまたは関数がデータのみを生成することを指定できます。これは、ユーザーが作成またはテストしたコマンドレットや関数を Data セクションに含めることができるように設計されています。
SupportedCommand の値は、1 つまたは複数のコマンドレット名または関数名のコンマ区切りリストです。
たとえば、次の Data セクションには、XML ファイル内のデータの書式を設定するユーザー定義のコマンドレット (Format-XML) が含まれています。
DATA -supportedCommand Format-XML
{
Format-XML -strings string1, string2, string3
}
Data セクションの使用
Data セクションの内容を使用するには、それを変数に代入し、変数表記を使用してその内容にアクセスします。
たとえば、次の Data セクションには、ヒア文字列をハッシュ テーブルに変換する ConvertFrom-StringData コマンドが含まれています。このハッシュ テーブルは $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 コマンドレットを使用する一重引用符付きのヒア文字列:
DATA {
ConvertFrom-StringData -stringdata @'
Text001 = Windows 7
Text002 = Windows Server 2008 R2
'@
}
ConvertFrom-StringData コマンドレットを使用する二重引用符付きのヒア文字列:
DATA {
ConvertFrom-StringData -stringdata @"
Msg1 = To start, press any key.
Msg2 = To exit, type "quit".
"@
}
データを生成するユーザー定義のコマンドレットを含む 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