以有效的方式将哈希表转换回字符串数据 [英] Convert hashtable back to string data in efficient way
本文介绍了以有效的方式将哈希表转换回字符串数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以一种有效的方式将哈希表转换回键值对。目前,我正在使用以下代码:
I am trying to convert a hashtable back to key-value pair in an efficient way. Currently I am using this:
$kv = ""
$hash.GetEnumerator() | ForEach {
$kv += "$($_.Name)=$($_.Value)"
}
没有任何方法可以直接将哈希表转换为键值对,或者我的意思是字符串数据。有 ConvertFrom-StringData
可以将键值对转换为哈希表。是否没有相反的方法,直接将哈希表转换为键值对?
Isn't there any way to directly convert hash table to key value pairs, or I mean string data. There is ConvertFrom-StringData
to convert key value pairs to hash table. Isn't there any way to do the opposite, convert hash tables to key value pairs directly?
EG(键值对)
a=1
b=2
c=3
推荐答案
我建议此脚本cmdlet用于类似的问题:
I suggested this script cmdlet for this similar question:
function ConvertTo-StringData {
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[HashTable[]]$HashTable
)
process {
foreach ($item in $HashTable) {
foreach ($entry in $item.GetEnumerator()) {
"{0}={1}" -f $entry.Key, $entry.Value
}
}
}
}
示例:
ConvertTo-StringData $hash
# or
$hash | ConvertTo-StringData
这篇关于以有效的方式将哈希表转换回字符串数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文