在Powershell中输出数组的哈希表

发布于 2024-10-28 11:36:08 字数 1896 浏览 2 评论 0原文

我已经束手无策了。我是 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

现在,经历了这一切之后,我仍然得到这个结果:

发送日期 PSV_类型
--------- ---------
20091201 {31、分配、AUDIT_TRAIL、书籍...}
20091202 {31、分配、AUDIT_TRAIL、书籍...}
20091203 {31、分配、AUDIT_TRAIL、书籍...}
20091204 {31、分配、AUDIT_TRAIL、书籍...}
20091207 {31、分配、AUDIT_TRAIL、书籍...}
20091208 {31、分配、AUDIT_TRAIL、书籍...}
20091209 {31、分配、AUDIT_TRAIL、书籍...}
20091210 {31、分配、AUDIT_TRAIL、书籍...}
20091211 {31、分配、AUDIT_TRAIL、书籍...}
20091214 {31、分配、AUDIT_TRAIL、书籍...}
20091215 {31、分配、AUDIT_TRAIL、书籍...}

更了解 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, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2024-11-04 11:36:08

由于这个原因,您不应该使用 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

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
等往事风中吹 2024-11-04 11:36:08

好吧,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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文