【发布时间】:2020-07-29 16:00:03
【问题描述】:
我想将一个哈希表写入一个文件,其中包含一个数组作为哈希表项之一。我的数组项被写出,但它包含 files=System.Object[]
注意 - 一旦成功,我会想反转这个过程并再次读回哈希表。
clear-host
$resumeFile="c:\users\paul\resume.log"
$files = Get-ChildItem *.txt
$files.GetType()
write-host
$types="txt"
$in="c:\users\paul"
Remove-Item $resumeFile -ErrorAction SilentlyContinue
$resumeParms=@{}
$resumeParms['types']=$types
$resumeParms['in']=($in)
$resumeParms['files']=($files)
$resumeParms.GetEnumerator() | ForEach-Object {"{0}={1}" -f $_.Name,$_.Value} | Set-Content $resumeFile
write-host "Contents of $resumefile"
get-content $resumeFile
结果
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
Contents of c:\users\paul\resume.log
files=System.Object[]
types=txt
in=c:\users\paul
【问题讨论】:
-
使用
Export-CliXmlcmdlet - 这就是它的用途。 [咧嘴] -
这很好,我喜欢它并且我可以这样做,但我正在尝试创建一个用户可以轻松读取和修改的简单文件。基于 XML 的输入/输出并不总是用户友好的。如果这是唯一的选择,那么我可能必须将文件数组作为 XML 写出到单独的文件中。谢谢李
-
不客气! [grin] ///// 所以,如果 XML 对人们来说太多了,你尝试过 JSON 吗?我对这种格式没有任何经验,但它被设计成比 XML 更人性化的文本格式。
标签: arrays powershell hashtable