在Powershell中输出数组的哈希表

时间:2011-03-31 00:54:14

标签: arrays powershell hashtable ellipsis

我在我的智慧结束。我是powershell的新手,我已经尝试过我在网上找到的所有内容。我想要做的是将数组的哈希表打印到文件中,而不会在每个数组值的末尾出现愚蠢的省略号。以下是我最好的尝试。

# 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

在所有这些之后,我仍然得到这个结果:

  

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

有人对powershell的方式更加明智,请告诉我我缺少的东西。如何在最后摆脱这些血腥的省略号,只要编写阵列的所有成员,无论有多少?我是否必须通过构建一个大的字符串缓冲区并将其输出到文件来滚动一些贫民窟解决方案,还是有更好的方法来做到这一点?

感谢。

2 个答案:

答案 0 :(得分:8)

出于这个原因,你不应该使用Out-File,它会运行默认的格式化引擎。你想要使用的是像这样的Set-Content / Add-Content。

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

答案 1 :(得分:2)

好的,powershell不会很好玩。这是我唯一可以开展的工作:

$lookupTable = $lookupTable.GetEnumerator() | Sort-Object Name 

foreach ($element in $lookupTable)
{
    $msg = $element.Key + "`t"
    foreach ($psv in $element.Value)
    {
        $msg = $msg + $psv + ","
    }
    #remove last comma and add newline
    $msg = $msg.SubString(0, $msg.length -1) + "`n"

    Add-Content C:\hashFiles.txt $msg
}