关于数据部分

简短说明

介绍数据部分,这些节将文本字符串和其他只读数据与脚本逻辑隔离开来。

详细说明

为 PowerShell 设计的脚本可以有一个或多个仅包含数据的 Data 节。 可以在任何脚本、函数或高级函数中包含一个或多个 Data 节。 “数据”部分的内容仅限于 PowerShell 脚本语言的指定子集。

将数据与代码逻辑分开可以更轻松地识别和管理逻辑和数据。 它允许为文本提供单独的字符串资源文件,例如错误消息和帮助字符串。 它还隔离代码逻辑,从而促进安全性和验证测试。

在 PowerShell 中,“数据”部分用于支持脚本国际化。 可以使用数据部分更轻松地隔离、查找和处理将转换为许多用户界面 (UI) 语言的字符串。

“数据”部分是 PowerShell 2.0 的一项功能。 没有修订的 PowerShell 1.0 中将不会运行包含数据部分的脚本。

语法

Data 节的语法如下所示:

DATA [<variable-name>] [-supportedCommand <cmdlet-name>] {
    <Permitted content>
}

需要数据关键字 (keyword) 。 此名称不区分大小写。 允许的内容仅限于以下元素:

  • 所有 PowerShell 运算符,但除外 -match

  • IfElseElseIf 语句

  • 以下自动变量: $PsCulture$PsUICulture$True$False$Null

  • 注释

  • 管道

  • 用分号 (;) 分隔的语句

  • 文本,如下所示:

    a
    1
    1,2,3
    "PowerShell 2.0"
    @( "red", "green", "blue" )
    @{ a = 0x1; b = "great"; c ="script" }
    [XML] @'
    <p> Hello, World </p>
    '@
    
  • Data 节中允许的 Cmdlet。 默认情况下, ConvertFrom-StringData 仅允许使用 cmdlet。

  • 使用 -SupportedCommand 参数在 Data 节中允许的 Cmdlet。

在 Data 节中使用 ConvertFrom-StringData cmdlet 时,可以将键值对括在单引号或双引号字符串中,或者用单引号或双引号的 here-strings 括起来。 但是,包含变量和子表达式的字符串必须括在单引号字符串或单引号的 here-string 中,这样变量就不会展开,子表达式不可执行。

-SupportedCommand

参数 -SupportedCommand 允许指示 cmdlet 或函数仅生成数据。 它旨在允许用户在已编写或测试的数据节中包含 cmdlet 和函数。

的值 -SupportedCommand 是一个或多个 cmdlet 或函数名称的逗号分隔列表。

例如,以下数据部分包括用户编写的 cmdlet, Format-XML用于设置 XML 文件中的数据的格式:

DATA -supportedCommand Format-Xml
{
    Format-Xml -Strings string1, string2, string3
}

使用数据部分

若要使用 Data 节的内容,请将其分配给变量,并使用变量表示法访问内容。

例如,以下数据部分包含将 ConvertFrom-StringData here-string 转换为哈希表的命令。 哈希表分配给变量 $TextMsgs

变量 $TextMsgs 不是数据节的一部分。

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

若要访问 中的哈希表中 $TextMsgs的键和值,请使用以下命令。

$TextMsgs.Text001
$TextMsgs.Text002

或者,可以将变量名称放在“数据”部分的定义中。 例如:

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

$TextMsgs

结果与前面的示例相同。

Name                           Value
----                           -----
Text001                        Windows 7
Text002                        Windows Server 2008 R2

示例

简单数据字符串。

DATA {
    "Thank you for using my 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 字符串:

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

使用 ConvertFrom-StringData cmdlet 的双引号 here 字符串:

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

包含生成数据的用户编写的 cmdlet 的数据部分:

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