在 Powershell 中将字符串转换为哈希表
Convert a String into a Hashtable in Powershell
我的要求有点特殊,但很容易理解。
我得到一个字符串,我想将其用作哈希表,但我做不到,我的格式非常特殊,我还没有成功使用 ConvertFrom-StringData 等方法。
格式预览:
@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}
因此,我希望能够访问 name
属性的内容。
我在那里花了很多时间但没有成功......
感谢您的帮助
因此您可以进行字符串解析并结合使用拆分和修剪。
$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"
#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
$obj = @{}
foreach ($kv in $preparedText.Split(";")) {
#Get key from string (which is "key = value")
$key = $kv.Trim().Split("=")[0].Trim()
Write-Host "Key = $key"
#Get value from string
$value = $kv.Trim().Split("=")[1].Trim()
Write-Host "Value = $value"
#Assign key-object pair to hashtable
$obj[$key] = $value
}
Write-Host "obj.name = $($obj.name);`nobj.id = $($obj.id);`nobj.privileges = $($obj.privileges)"
哪个returns:
obj.name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0;
obj.id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229;
obj.privileges =
另一种方法是使用 convertfrom-stringdata cmdlet,记录在此处:
Link to official MS documentation
The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign (=) character.
使用@Karolina Ochlik 中的 OP 示例 post:
$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"
#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
# replace semicolon with newline - `n needs double quotes
$preparedText.replace(';',"`n") | ConvertFrom-StringData
这将创建输出:
Name Value
---- -----
name 7.10 10-16 - 6.0 * * * * | 1 0 0 0
id 04cc943e-14a8-4bf6-8601-bd6fde3cc229
privileges
真正的工作在一行中完成
$preparedText.replace(';',"`n") | ConvertFrom-StringData
我希望这对某人有所帮助。
我的要求有点特殊,但很容易理解。
我得到一个字符串,我想将其用作哈希表,但我做不到,我的格式非常特殊,我还没有成功使用 ConvertFrom-StringData 等方法。
格式预览:
@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}
因此,我希望能够访问 name
属性的内容。
我在那里花了很多时间但没有成功......
感谢您的帮助
因此您可以进行字符串解析并结合使用拆分和修剪。
$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"
#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
$obj = @{}
foreach ($kv in $preparedText.Split(";")) {
#Get key from string (which is "key = value")
$key = $kv.Trim().Split("=")[0].Trim()
Write-Host "Key = $key"
#Get value from string
$value = $kv.Trim().Split("=")[1].Trim()
Write-Host "Value = $value"
#Assign key-object pair to hashtable
$obj[$key] = $value
}
Write-Host "obj.name = $($obj.name);`nobj.id = $($obj.id);`nobj.privileges = $($obj.privileges)"
哪个returns:
obj.name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0;
obj.id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229;
obj.privileges =
另一种方法是使用 convertfrom-stringdata cmdlet,记录在此处: Link to official MS documentation
The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign (=) character.
使用@Karolina Ochlik 中的 OP 示例 post:
$text = "@{id = 04cc943e-14a8-4bf6-8601-bd6fde3cc229; name = 7.10 10-16 - 6.0 * * * * | 1 0 0 0; privileges =}"
#this is to get rid of @{}
$preparedText = $text.Substring(2,$text.Length-3)
# replace semicolon with newline - `n needs double quotes
$preparedText.replace(';',"`n") | ConvertFrom-StringData
这将创建输出:
Name Value
---- -----
name 7.10 10-16 - 6.0 * * * * | 1 0 0 0
id 04cc943e-14a8-4bf6-8601-bd6fde3cc229
privileges
真正的工作在一行中完成
$preparedText.replace(';',"`n") | ConvertFrom-StringData
我希望这对某人有所帮助。