ConvertFrom-StringData

将包含一个或多个键-值对的字符串转换为哈希表。

语法

ConvertFrom-StringData
                [-StringData] <String>
                [[-Delimiter] <Char>]
                [<CommonParameters>]

说明

ConvertFrom-StringData cmdlet 可将包含一个或多个键值对的字符串转换为哈希表。 由于每个键值对必须位于一个单独的行上,所以经常使用 here-string 作为输入格式。 默认情况下,键必须用等号 (=) 字符与值分隔。

可将ConvertFrom-StringData cmdlet 视为可在脚本或函数的 DATA 节中使用的安全 cmdlet。 在 DATA 节中使用时,字符串的内容必须遵循 DATA 节的规则。 有关详细信息,请参阅 about_Data_Sections

ConvertFrom-StringData 支持传统机器翻译工具允许的转义字符序列。 即该 cmdlet 可以通过使用 Regex.Unescape 方法将字符串数据中的反斜杠 (\) 解释为转义字符,而不是解释为通常表示脚本中某一行的结尾的 PowerShell 反撇号字符 (`)。 在 here-string 中,反撇号字符无效。 还可以通过使用前导反斜杠对文本反斜杠进行转义,保留结果中的文本反斜杠,如下所示:\\。 未转义的反斜杠字符(例如那些通常用在文件路径中的反斜杠字符)可以在结果中呈现为非法的转义序列。

PowerShell 7 添加了 Delimiter 参数。

示例

示例 1:将带单引号的 here-string 转换为哈希表

此示例将用户消息中带单引号的 here-string 转换为一个哈希表。 在带单引号的字符串中,不能使用变量和无法计算的表达式来代替其值。 ConvertFrom-StringData cmdlet 将 $Here 变量中的值转换为哈希表。

$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
ConvertFrom-StringData -StringData $Here

Name                           Value
----                           -----
Msg3                           The specified variable does not exist.
Msg2                           Credentials are required for this command.
Msg1                           The string parameter is required.

示例 2:使用不同的分隔符转换字符串数据

此示例演示如何转换使用不同字符作为分隔符的字符串数据。 在此示例中,字符串数据使用管道字符 (|) 作为分隔符。

$StringData = @'
color|red
model|coupe
year|1965
condition|mint
'@
$carData = ConvertFrom-StringData -StringData $StringData -Delimiter '|'
$carData

Name                           Value
----                           -----
condition                      mint
model                          coupe
color                          red
year                           1965

示例 3:转换包含注释的 here-string

此示例将包含一条注释和多个键值对的 here-string 转换为一个哈希表。

ConvertFrom-StringData -StringData @'
Name = Disks.ps1

# Category is optional.

Category = Storage
Cost = Free
'@

Name                           Value
----                           -----
Cost                           Free
Category                       Storage
Name                           Disks.ps1

StringData 参数的值是一个 here-string,而不是一个包含 here-string 的变量。 两种格式都有效。 here-string 包括有关某字符串的注释。 ConvertFrom-StringData 忽略单行注释,但 # 字符必须是行中的第一个非空格字符。 将忽略 # 后面一行中的所有字符。

示例 4:将字符串转换为哈希表

此示例将一个带双引号的常规字符串(非 here-string)转换为一个哈希表,并将其保存在 $A 变量中。

$A = ConvertFrom-StringData -StringData "Top = Red `n Bottom = Blue"
$A

Name             Value
----             -----
Bottom           Blue
Top              Red

为了满足每个键值对都必须在一个单独的行上的条件,字符串使用 PowerShell 换行符 (`n) 来分隔这些对。

示例 5:在脚本的 DATA 节中使用 ConvertFrom-StringData

此示例显示了在脚本的 DATA 节中使用的 ConvertFrom-StringData 命令。 DATA 节下面的语句向用户显示该文本。

$TextMsgs = DATA {
ConvertFrom-StringData @'
Text001 = The $Notebook variable contains the name of the user's system notebook.
Text002 = The $MyNotebook variable contains the name of the user's private notebook.
'@
}
$TextMsgs

Name             Value
----             -----
Text001          The $Notebook variable contains the name of the user's system notebook.
Text002          The $MyNotebook variable contains the name of the user's private notebook.

由于文本包括变量名称,所以必须用单引号将它括起来,以便按照字义解释变量,而不是展开它。 在 DATA 节中允许使用变量。

示例 6:使用管道运算符传递字符串

此示例表明可使用管道运算符 (|) 将字符串发送给 ConvertFrom-StringData。 变量的值 $Here 通过管道传递给 ConvertFrom-StringData 变量和结果 $Hash

$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
$Hash = $Here | ConvertFrom-StringData
$Hash

Name     Value
----     -----
Msg3     The specified variable does not exist.
Msg2     Credentials are required for this command.
Msg1     The string parameter is required.

示例 7:使用转义字符添加新行并返回字符

此示例显示了如何使用转义字符创建新行并返回源数据中的字符。 将使用转义序列 \n 在与名称或生成的哈希表中的项相关联的文本块中创建新行。

ConvertFrom-StringData @"
Vincentio = Heaven doth with us as we with torches do,\nNot light them for themselves; for if our virtues\nDid not go forth of us, 'twere all alike\nAs if we had them not.
Angelo = Let there be some more test made of my metal,\nBefore so noble and so great a figure\nBe stamp'd upon it.
"@ | Format-List

Name  : Angelo
Value : Let there be some more test made of my metal,
        Before so noble and so great a figure
        Be stamp'd upon it.

Name  : Vincentio
Value : Heaven doth with us as we with torches do,
        Not light them for themselves; for if our virtues
        Did not go forth of us, 'twere all alike
        As if we had them not.

示例 8:使用反斜杠转义字符正确呈现文件路径

此示例显示了如何使用字符串数据中的反斜杠转义字符,以使文件路径正确呈现在生成的 ConvertFrom-StringData 哈希表中。 双反斜杠可确保文本反斜杠字符正确呈现在哈希表输出中。

ConvertFrom-StringData "Message=Look in c:\\Windows\\System32"

Name                           Value
----                           -----
Message                        Look in c:\Windows\System32

参数

-Delimiter

用于将键与要转换的字符串中的值数据分隔的字符。 默认分隔符是等号 (=) 字符。 此参数已在 PowerShell 7 中添加。

Type:Char
Position:1
Default value:'='
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-StringData

指定要转换的字符串。 可使用该参数或通过管道将字符串传递给 ConvertFrom-StringData。 参数名为可选项。

此参数的值必须是包含一个或多个键值对的字符串。 每个键值对都必须位于一个单独的行上,或每对都必须由换行符 (`n) 分隔。

字符串中可包括注释,但是注释不能以键值对的形式位于同一行上。 ConvertFrom-StringData 忽略单行注释。 # 字符必须是行中的第一个非空格字符。 将忽略 # 后面一行中的所有字符。 哈希表中不包括注释。

here-string 是包含一行或多行的字符串。 here-string 中的引号在文本上解释为字符串数据的一部分。 有关详细信息,请参阅 about_Quoting_Rules

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

输入

String

可以通过管道将包含一个键值对的字符串传递给此 cmdlet。

输出

Hashtable

此 cmdlet 返回它基于键值对创建的哈希表。

备注

here-string 是由一行或多行组成的字符串,在其中,按照字义解释引号。

在以多种口语显示用户消息的脚本中,此 cmdlet 非常有用。 可使用字典风格的哈希表来从代码中隔离文本字符串(如在资源文件中),并为文本字符串设置格式以便在转换工具中使用。