ConvertFrom-StringData

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

语法

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

说明

ConvertFrom-StringData cmdlet 将包含一个或多个键和值对的字符串转换为哈希表。 由于每个键/值对必须位于单独的行上,因此此处字符串通常用作输入格式。

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

ConvertFrom-StringData 支持传统机器翻译工具允许的转义字符序列。 也就是说,cmdlet 可以使用 Regex.Unescape 方法将反斜杠(\)解释为字符串数据中的转义字符,而不是通常向脚本中的行尾发出信号的 Windows PowerShell 反斜杠字符(')。 在此处字符串中,反杆字符不起作用。 还可以通过在结果中转义文本反斜杠来保留文本反斜杠,如下所示:\\。 未转义的反斜杠字符(如文件路径中常用的反斜杠字符)可以在结果中呈现为非法转义序列。

示例

示例 1:将单引号的此处字符串转换为哈希表

PS C:\> $Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
PS C:\> 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.

这些命令将单引号的此处用户消息字符串转换为哈希表。 在单引号字符串中,值不会替换为变量,并且不会计算表达式。

第一个命令创建一个此处字符串并将其保存在$Here变量中。

第二个命令使用 ConvertFrom-StringData cmdlet 将$Here变量中的此处字符串转换为哈希表。

示例 2:将双引号的此处字符串转换为哈希表

PS C:\> $P = @"
ISE = Windows PowerShell Integrated Scripting Environment
"@
PS C:\> $P | Get-Member
TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
Clone            Method                System.Object Clone()

PS C:\> $Hash = ConvertFrom-StringData -StringData $P
PS C:\> $Hash | Get-Member
TypeName: System.Collections.Hashtable
Name              MemberType            Definition
----              ----------            ----------
Add               Method                System.Void Add(Object key, Object

这些命令演示了 ConvertFrom-StringData 实际上将此处的字符串转换为哈希表。

第一个命令创建包含一个键/值对的双引号此处字符串,并将其保存在$P变量中。

第二个命令使用管道运算符 (|) 将$P变量发送到 Get-Member cmdlet。 结果显示$P是字符串(System.String)。

第三个命令使用 ConvertFrom-StringData cmdlet 将$P中的此处字符串转换为哈希表。 该命令将结果存储在$Hash变量中。

最终命令使用管道运算符 (|) 将$Hash变量发送到 Get-Member cmdlet。 结果显示$Hash变量的内容是哈希表(System.Collections.Hashtable)。

示例 3:将此处字符串转换为哈希表

PS C:\> ConvertFrom-StringData -StringData @'
Name = Disks.ps1

# Category is optional.

Category = Storage
Cost = Free
'@
Name                           Value
----                           -----
Cost                           Free
Category                       Storage
Name                           Disks.ps1

此命令将包含多个键/值对的单引号此处字符串转换为哈希表。

在此命令中,StringData 参数的值是此处字符串,而不是包含此处字符串的变量。 任一格式都有效。

此处的字符串包含有关其中一个字符串的注释。 注释在字符串中有效,前提是批注与键/值对位于不同的行上。

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

PS C:\> $A = ConvertFrom-StringData -StringData "Top = Red `n Bottom = Blue"
PS C:\> "Top = " + $A.Top
Top = Red PS C:\> "Bottom = " + $A.Bottom
Bottom = Blue

此示例将常规双引号字符串(而不是此处字符串)转换为哈希表,并将其保存在$A变量中。

若要满足每个键/值对必须位于单独行的条件,它使用 Windows PowerShell 换行符 ('n) 分隔对。

结果是输入的哈希表。 其余命令显示输出。

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

PS C:\> $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.
'@
}
PS C:\> $TextMsgs.Text001
The $Notebook variable contains the name of the user's system notebook. PS C:\> $TextMsgs.Text002
The $MyNotebook variable contains the name of the user's private notebook.

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

由于文本包含变量名称,因此它必须括在单引号字符串中,以便以字面形式解释变量,而不是展开变量。 DATA 节中不允许使用变量。

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

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

此示例演示如何使用管道运算符 (|) 将字符串发送到 convertFrom-StringData

第一个命令将此处字符串保存在$Here变量中。 第二个命令使用管道运算符 (|) 将$Here变量发送到 ConvertFrom-StringData。 该命令将结果保存在$Hash变量中。

最后一个命令显示$Hash变量的内容。

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

PS C:\> 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.

此示例演示如何使用转义字符创建新行并在 ConvertFrom-StringData中返回 字符。 在此示例中,转义序列 \n 用于在文本块(生成的哈希表中的值)中创建新行,该行与名称或项(结果哈希表中的名称)相关联。

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

PS C:\> ConvertFrom-StringData "Message=Look in c:\\Windows\\System32"
Name                           Value
----                           -----
Message                        Look in c:\Windows\System32

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

参数

-StringData

指定要转换的字符串。 可以使用此参数或管道将字符串 ConvertFrom-StringData。 参数名称是可选的。

此参数的值必须是用单引号括起来的字符串、用双引号括起来的字符串,或者是包含一个或多个键/值对的此处字符串。 每个键/值对必须位于单独的行上,或者每个对必须用换行符('n)分隔。

可以在字符串中包含注释,但注释不能与键/值对位于同一行。 注释不包括在哈希表中。

此处字符串是一个字符串,其中包含一行或多行,其中引号按字面解释。 有关详细信息,请参阅about_Quoting_Rules。

类型:String
Position:0
默认值:None
必需:True
接受管道输入:True
接受通配符:False

输入

String

可以通过管道将包含键/值对的字符串传递给 ConvertFrom-StringData

输出

Hashtable

此 cmdlet 返回它从键/值对创建的哈希表。

备注

  • 此处字符串是一个字符串,其中包含一行或多行,其中引号按字面解释。

    此 cmdlet 可用于以多种口语显示用户消息的脚本。 可以使用字典样式哈希表将文本字符串与代码(如资源文件中)隔离,并设置文本字符串的格式,以便在翻译工具中使用。