Can someone please explain Why my first examples don't work, and why adding in a ForEach-Object solves the problem? Thanks in advance!
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
only logs a single row of System.Collections.Hashtable
. So, I've also tried
$ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt
and end up with rows of
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" }
and end up with
Server address : System.Collections.Hashtable.Server address
Client address : System.Collections.Hashtable.Client address
User name : System.Collections.Hashtable.User name
Solved with:
$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 :)
In your first example
$ht | Add-Content 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.
Next
$ht.GetEnumerator() | Sort-Object Name | Add-Content log.txt
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.
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.