Powershell哈希表未按预期写入文件-仅接收"System.Collections"行数 [英] Powershell hashtable does not write to file as expected - receive only "System.Collections" rows

查看:72
本文介绍了Powershell哈希表未按预期写入文件-仅接收"System.Collections"行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释 为什么 我的第一个示例不起作用,为什么添加ForEach-Object可以解决问题?预先感谢!

Can someone please explain Why my first examples don't work, and why adding in a ForEach-Object solves the problem? Thanks in advance!

我将命令的返回结果解析为哈希表(帖子末尾的示例),并希望将信息记录到文件中作为我的处理的一部分.我知道$ht.GetEnumerator() | Sort-Object Name会将完整的哈希返回到屏幕,进行排序.但是,一旦我尝试将内容发送到文件中,它就会中断.

I parsed the return from a command into a hashtable (sample at end of post) and want to log the information to a file as part of my processing. I know that $ht.GetEnumerator() | Sort-Object Name will return the full hash to screen, sorted. However, once I try sending things to file, it breaks.

$ht | Add-Content log.txt

仅记录System.Collections.Hashtable的一行.因此,我也尝试过

only logs a single row of System.Collections.Hashtable. So, I've also tried

$ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt 

并以

System.Collections.DictionaryEntry
System.Collections.DictionaryEntry
System.Collections.DictionaryEntry

因此,我尝试遍历并分别使用

So then I tried to loop through and handle each individually with

foreach ($key in $ht.keys) {
Add-Content log.txt "$key : $ht.$key" }

并以

Server address : System.Collections.Hashtable.Server address
Client address : System.Collections.Hashtable.Client address
User name : System.Collections.Hashtable.User name

已解决:

$ht.GetEnumerator() | Sort-Object Name |
ForEach-Object {"{0} : {1}" -f $_.Name,$_.Value} |
Add-Content log.txt 


哈希表示例供参考:


For reference, the hashtable sample:

$ht = @{
    "Server address" = "server.net";
    "Client address" = "10.20.121.153";
    "User name" = "myuser"
}

推荐答案

回答为什么部分,您显然有解决方案:)

Answering the why part, you obviously have a solution :)

在第一个示例中

$ht | Add-Content log.txt

PowerShell使用$ht并尝试以某种方式将其转换为字符串,以便可以通过Add-Content存储它.因为没有为哈希表定义任何转换,所以仅从转换返回类型名称.与例如new-Object Random|Add-Content d:\log.txt相同.同样,只写类型名称.

PowerShell takes $ht and tries to somehow convert it to a string so that it can be stored via Add-Content. Because there is no conversion defined for the hashtable, only the type name is returned from the conversion. Same as for example new-Object Random|Add-Content d:\log.txt. Again, only type name is written.

下一步

$ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt 

是相似的. GetEnumerator返回用于迭代的对象;返回类型为System.Collections.DictionaryEntry的对象.同样,没有转换为字符串,因此返回类型名称.

is similar. GetEnumerator returns object that is used for iteration; objects of type System.Collections.DictionaryEntry are returned. Again, there is no conversion to string, so type names are returned.

就我个人而言,我认为PowerShell应该足够聪明,并在这里提供帮助.问题是如何?".设计人员可能不想对输出进行硬编码.可能是"{key}: {value}""{key} = {value}""{key}/{value}"或...格式不清楚,因此他们将其留给我们来决定和设置格式,就像您使用foreach语句所做的那样.

Personally, I think PowerShell should be smart enough and help here. The question is "how?". Designers probably didn't want to hardcode the output. It might be "{key}: {value}" or "{key} = {value}", or "{key}/{value}", or ... The format is not clear, so they left it for us to decide and format as you did it with the foreach statement.

这篇关于Powershell哈希表未按预期写入文件-仅接收"System.Collections"行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆