在Powershell中输出数组的哈希表

问题描述:

我精疲力尽。我是PowerShell的新手,我已经尝试了所有可以在此主题上在线找到的内容。我想做的是将数组的哈希表打印到文件中,而每个数组值的末尾都不会出现愚蠢的省略号。以下是我的最佳尝试。

I am at my wits end. I am new to powershell and I have tried everything I have been able to find on this subject online. What I am trying to do is print a hashtable of arrays to a file without the stupid ellipsis appearing at the end of each array value. Below is my best attempt.

# Update output buffer size to prevent clipping in output window.
if( $Host -and $Host.UI -and $Host.UI.RawUI ) 
{
   $rawUI = $Host.UI.RawUI
   $oldSize = $rawUI.BufferSize
   $typeName = $oldSize.GetType( ).FullName
   $newSize = New-Object $typeName (1000, $oldSize.Height)
   $rawUI.BufferSize = $newSize
}

# Suposedly to allow enumeration in formatting to be unlimited
$formatenumerationlimit = -1

$Dir = get-childitem c:\SomeFolderWithFiles -recurse
$List = $Dir | where {$_.extension -eq ".hash"} | where-object {$_ -ne $null}
$lookupTable = @{}
Foreach ($element in $List)
{
    #Get the type of file from filename
    $PSV_Type = $element.Name.Substring(0, $element.Name.indexOf("."))
    #Get the date sent from filename
    $Date_Sent = $element.Name.Substring($element.Name.length - 20,8)

    #Populate hashTable
    .....
}
$columns = @{Expression={$_.Name};Label="Date_Sent";width=12}, @{Expression={$_.Value};Label="PSV_Types";width=1000}
$lookupTable.GetEnumerator() | Sort-Object Name | Format-Table $columns | out-file C:\hashFiles.txt -width 1012

现在毕竟,我仍然得到结果是:

Now after all this, I still get this as a result:


Date_Sent PSV_Types

--------- ----- ----

20091201 {31,分配,审核_跟踪,预订...}

20091202 {31,分配,审核_跟踪,预订...}

20091203 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091204 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091207 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091208 {31,分配,AUDIT_TRAIL,书...}

20091209 {31,分配,AUDIT_TRAIL,书...}

20091210 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091211 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091214 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

20091215 {31,ALLOCATIONS,AUDIT_TRAIL,BOOKS ...}

Date_Sent PSV_Types
--------- ---------
20091201 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091202 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091203 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091204 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091207 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091208 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091209 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091210 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091211 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091214 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}
20091215 {31, ALLOCATIONS, AUDIT_TRAIL, BOOKS...}

有人明智道路powershell的功能,请告诉我我所缺少的。我如何摆脱这些血腥的省略号,而不论数组有多少,只写数组的所有成员?

Someone wiser in the ways of powershell please tell me what I am missing. How do I get rid of these bloody ellipsis at the end and just write all the members of the array no matter how many there are? Do I have to just roll some ghetto solution by building a big string buffer and outputting that to a file or is there a better way to do this?

谢谢。

谢谢。

谢谢。 p>

Thanks.

出于这个原因,您不应该使用Out-File,它通过默认格式设置引擎运行。您想要使用的是这样的Set-Content / Add-Content。

You shouldn't use Out-File for this reason, it runs through the default formatting engine. What you want to use is Set-Content/Add-Content like this.

$lookupTable.GetEnumerator() | Sort-Object Name |
    ForEach-Object {"{0}`t{1}" -f $_.Name,($_.Value -join ",")} |
    Add-Content C:\hashFiles.txt