ConvertFrom-StringData
将包含一个或多个键-值对的字符串转换为哈希表。
语法
ConvertFrom-StringData [-StringData] <string> [<CommonParameters>]
说明
ConvertFrom-StringData cmdlet 将包含一个或多个键-值对的字符串转换为哈希表。由于每个键-值对必须位于一个单独的行上,所以经常使用 here-string 作为输入格式。
可将 ConvertFrom-StringData cmdlet 视为可在脚本或函数的 DATA 节中使用的安全 cmdlet。在 DATA 节中使用时,字符串的内容必须遵循 DATA 节的规则。有关详细信息,请参阅 about_Data_Sections。
参数
-StringData <string>
指定要转换的字符串。可使用该参数或通过管道将字符串传递至 ConvertFrom-StringData。参数名为可选项。
该参数的值必须是括在单引号中的字符串(单引号字符串)或括在双引号中的字符串(双引号字符串),或包含一个或多个键-值对的 here-string。每个键-值对必须位于一个单独的行上,或每个键-值对必须由换行符 (`n) 分隔。
字符串中可包括注释,但是注释不能以键-值对的形式位于同一行上。哈希表中不包括注释。
here-string 是由一行或多行组成的字符串,在其中,按照字义解释引号。有关详细信息,请参阅 about_Quoting_Rules。
是否为必需? |
true |
位置? |
1 |
默认值 |
|
是否接受管道输入? |
true (ByValue) |
是否接受通配符? |
false |
<CommonParameters>
此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.
输入和输出
输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。
输入 |
System.String 可将包含一个键-值对的字符串通过管道传送至 ConvertFrom-StringData。 |
输出 |
System.Collections.Hashtable ConvertFrom-StringData 返回它基于键-值对创建的哈希表。 |
说明
here-string 是由一行或多行组成的字符串,在其中,按照字义解释引号。有关详细信息,请参阅 about_Quoting_Rules。
在以多种口语显示用户消息的脚本中,ConvertFrom-StringData 非常有用。可使用字典风格的哈希表来从代码中隔离文本字符串(如在资源文件中),并为文本字符串设置格式以便在转换工具中使用。
示例 1
C:\PS>$here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
C:\PS> 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-string 转换为一个哈希表。在带单引号的字符串中,不能使用变量和无法计算的表达式来代替其值。
第一个命令将创建一个 here-string,并将它保存在 $here 变量中。
第二个命令使用 ConvertFrom-StringData cmdlet 将 $here 变量中的 here-string 转换为哈希表。
示例 2
C:\PS>$p = @"
ISE = Windows PowerShell Integrated Scripting Environment
"@
C:\PS> $p | get-member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
...
C:\PS> $hash = convertfrom-stringdata -stringdata $p
C:\PS> $hash | get-member
TypeName: System.Collections.Hashtable
Name MemberType Definition
---- ---------- ----------
Add Method System.Void Add(Object key, Object
...
说明
-----------
这些命令表明 ConvertFrom-StringData 实际上是将 here-string 转换为一个哈希表。
第一个命令创建一个带双引号的 here-string(该 here-string 包含一个键-值对),并将它保存在 $p 变量中。
第二条命令使用管道运算符 (|) 将 $p 变量发送至 Get-Member cmdlet。结果表明 $p 是一个字符串 (System.String)。
第三条命令使用 ConvertFrom-StringData cmdlet 将 $p 中的 here-string 转换为一个哈希表。该命令将结果存储在 $hash 变量中。
最后一个命令使用管道运算符 (|) 将 $hash 变量传送到 Get-Member cmdlet。结果表明 $hash 变量的内容是一个哈希表 (System.Collections.Hashtable)。
示例 3
C:\PS>convertfrom-stringdata -stringdata @'
Name = Disks.ps1
# Category is optional.
Category = Storage
Cost = Free
'@
Name Value
---- -----
Cost Free
Category Storage
Name Disks.ps1
说明
-----------
此命令将包含多个键-值对的带单引号的 here-string 转换为哈希表。
在此命令中,StringData 参数的值是一个 here-string,而不是一个包含 here-string 的变量。两种格式都有效。
here-string 包括有关某字符串的注释。假如注释与键-值对不在同一行上,则注释在字符串中是有效的。
示例 4
C:\PS>$a = convertfrom-stringdata -stringdata "Top = Red `n Bottom = Blue"
C:\PS> "Top = " + $a.Top
Top = Red
C:\PS> "Bottom = " + $a.Bottom
Bottom = Blue
说明
-----------
此示例将一个带双引号的常规字符串(非 here-string)转换为一个哈希表,并将其保存在 $a 变量中。
为了满足每个键-值对必须在一个单独的行上的条件,它使用 Windows PowerShell 换行符 (`n) 来分隔这些对。
结果为一个由输入内容组成的哈希表。其余命令显示输出内容。
示例 5
C:\PS>$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.
'@
}
C:\PS> $TextMsgs.Text001
The $Notebook variable contains the name of the user's system notebook.
C:\PS> $TextMsgs.Text002
The $MyNotebook variable contains the name of the user's private notebook.
说明
-----------
此示例演示了在脚本的 DATA 节中使用的 ConvertFrom-StringData 命令。DATA 节下面的语句向用户显示该文本。
由于文本包括变量名称,所以必须用单引号将它括起来,以便按照字义解释变量,而不是展开它。在 DATA 节中允许使用变量。
示例 6
C:\PS>$here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
C:\PS> $hash = $here | convertfrom-stringdata
C:\PS> $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-string 保存在 $here 变量中。第二条命令使用管道运算符 (|) 将 $here 变量发送至 ConvertFrom-StringData。该命令将结果保存在 $hash 变量中。
最后一条命令显示 $hash 变量的内容。
另请参阅
概念
about_Data_Sections
about_quoting_rules
about_script_internationalization