[英]PowerShell, PSCustomObject, array format issue
I am trying to create array made by PSCustomObject
elements, but having some issues with it.我正在尝试创建由PSCustomObject
元素组成的数组,但遇到了一些问题。 Here is the simplified example of the code I have:这是我拥有的代码的简化示例:
$FineData = @()
foreach ($Session in $Sessions) {
$Job = Get-VBRTaskSession -Session $Session
$Result = [pscustomobject]@{
Id = $job.Id.Guid
}
$FineData += $Result
}
$FineData
The format it returns:它返回的格式:
Id
{19ea68a7-f2c3-4429-bc46-a87b4a295105, 3ebf8568-10ce-4608-bbb2-c80d06874173, 2ec28852-3f5e-477d-8742-872863e41b6d}
The format I want:我想要的格式:
Id : 19ea68a7-f2c3-4429-bc46-a87b4a295105
Id : 3ebf8568-10ce-4608-bbb2-c80d06874173
Id : 2ec28852-3f5e-477d-8742-872863e41b6d
Thanks in advance.提前致谢。
It seems like your Get-VBRTaskSession
could be returning more than one object, hence an inner loop is required:看起来您的Get-VBRTaskSession
可能返回多个对象,因此需要一个内部循环:
$FineData = foreach ($Session in $Sessions) {
foreach($guid in (Get-VBRTaskSession -Session $Session).Id.Guid) {
[pscustomobject]@{
Id = $guid
}
}
}
$FineData
You can then pipe $FineData
to Format-List
if you want it to be displayed as a list in your console:然后,如果您希望它在控制台中显示为列表,则可以通过管道将$FineData
给Format-List
:
$FineData | Format-List
As aside, adding to a fixed collection should be avoided, see this answer for details: Why should I avoid using the increase assignment operator (+=) to create a collection .另外,应避免添加到固定集合,有关详细信息,请参阅此答案: Why should I avoid using the increase assignment operator (+=) to create a collection 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.