Powershell 哈希表数组的格式表
我试图使用Powershell 哈希表数组的格式表,powershell,Powershell,我试图使用Format Table命令输出从TFS repo签出的所有文件的哈希表数组 到目前为止,我的代码是: $arr = @(); #Take the string from the tf command, parse it and build an array of hash tables (tf stat /recursive /user:* /format:detailed | Select-String -Pattern '^\$' -NotMatch | Select -Skip
Format Table
命令输出从TFS repo签出的所有文件的哈希表数组
到目前为止,我的代码是:
$arr = @();
#Take the string from the tf command, parse it and build an array of hash tables
(tf stat /recursive /user:* /format:detailed | Select-String -Pattern '^\$' -NotMatch | Select -SkipLast 3 | Out-String) -split '(\r\n){2}' | ForEach-Object {
$ht = @{};
if ($_ -ne '') {
$str = $_ | Out-String;
$str -split '\r?\n'| ForEach-Object {
$key, $value = $_ -split '\s*:\s*';
#Write-Host $key, $Value;
try {
$ht.Add($key, $value);
} catch [ArgumentException] {
Write-Host "Caught exception";
}
}
$arr += ($ht);
}
}
编辑
看来我在这里出错了
$arr.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize
完全错误:
Cannot convert value "System.Collections.Hashtable" to type
"System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". Error: "Cannot process argument because the
value of argument "name" is not valid. Change the value of the "name" argument and run the operation again."
At C:\Dev\Tools\powershell\Convert-TfsOutput.ps1:21 char:15
+ $arr.ForEach({[PSCustomObject]$_}) | Format-Table -AutoSize
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastConstructorException
Edit2
以下是我将上述行替换为以下内容时的示例输出:
$arr.ForEach({ $_ | Out-String })
Name Value
---- -----
Workspace work1
Date {Wednesday, September 5, 2018 1, 38, 48 PM}
Local item file1
File type Windows-1252
User user1
Lock none
Change edit
Name Value
---- -----
Workspace work2
Date {Monday, September 10, 2018 12, 14, 56 PM}
Local item file2
User user2
Lock none
Change edit
编辑3
下面命令的输出
Write-Host $str;
User : User1
Date : Wednesday, September 5, 2018 1:38:48 PM
Lock : none
Change : edit
Workspace : Work1
Local item : File1
File type : Windows-1252
User : User2
Date : Monday, September 10, 2018 12:14:56 PM
Lock : none
Change : edit
Workspace : Work2
Local item : File2
希望以表格格式输出,列名称下方有行:
Workspace | Date | Local item | File type | User | Lock | Change
工作区|日期|本地项|文件类型|用户|锁定|更改
试图在另一个答案中使用该代码,但输出不正确。
将哈希表转换为自定义对象,然后将其传递到
格式表
... | Where-Object { $_ } | ForEach-Object {
$ht = @{};
($_ | Out-String) -split '\r?\n'| ForEach-Object {
...
}
New-Object -Type PSObject -Property $ht
} | Format-Table
编辑:看起来您的输入数据有空行,这会导致哈希表中的键带有空字符串,这会导致您观察到的错误,因为对象不能有名称带有空字符串的属性 将哈希表/对象创建更改为以下内容:
... | Where-Object { $_ } | ForEach-Object {
$ht = ($_ | Out-String).Trim() -replace '\s+:\s+', '=' |
ConvertFrom-StringData
New-Object -Type PSObject -Property $ht
} | Format-Table
将哈希表转换为自定义对象,然后将其传递到
格式表
... | Where-Object { $_ } | ForEach-Object {
$ht = @{};
($_ | Out-String) -split '\r?\n'| ForEach-Object {
...
}
New-Object -Type PSObject -Property $ht
} | Format-Table
编辑:看起来您的输入数据有空行,这会导致哈希表中的键带有空字符串,这会导致您观察到的错误,因为对象不能有名称带有空字符串的属性 将哈希表/对象创建更改为以下内容:
... | Where-Object { $_ } | ForEach-Object {
$ht = ($_ | Out-String).Trim() -replace '\s+:\s+', '=' |
ConvertFrom-StringData
New-Object -Type PSObject -Property $ht
} | Format-Table
$pso=新对象-类型PSObject-属性$ht$arr+=($pso);}$arr.ForEach({[PSObject]${})Format Table-AutoSize尝试了此操作并获得异常:新对象:无法处理参数,因为参数“name”的值无效。更改“name”参数的值,然后再次运行该操作。@wonderstruck80
[PSObject]$\uu
应该完成什么?此时您已经有一个自定义对象列表。另外,不要附加到循环中的数组。抱歉,不确定如何正确设置注释的格式。看起来我在创建新对象时出错了,但不确定原因。这使我找到了初始问题的解决方案。$pso=新对象-类型PSObject-属性$ht$arr+=($pso);}$arr.ForEach({[PSObject]${})Format Table-AutoSize尝试了此操作并获得异常:新对象:无法处理参数,因为参数“name”的值无效。更改“name”参数的值,然后再次运行该操作。@wonderstruck80[PSObject]$\uu
应该完成什么?此时您已经有一个自定义对象列表。另外,不要附加到循环中的数组。抱歉,不确定如何正确设置注释的格式。看起来我在创建新对象时出错了,但不确定原因。这让我找到了最初问题的解决方案。