Output a hashtable of Arrays in Powershell

删除回忆录丶 提交于 2020-01-03 09:10:01

问题


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, 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...}

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?

Thanks.


回答1:


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



回答2:


Ok, powershell would not play nice. This is the only thing I could get to work:

$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
}


来源:https://stackoverflow.com/questions/5494201/output-a-hashtable-of-arrays-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!