在Powershell中输出数组的哈希表
我已经束手无策了。我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这个原因,您不应该使用 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.
好吧,powershell 的表现不太好。这是我唯一能开始工作的事情:
Ok, powershell would not play nice. This is the only thing I could get to work: