我有这个PSObject
(来自XML):
bool : {IsActive, ShowOnB2C, ShowOnB2B, IsWebNews}
str : {id, ProductId, GroupName, Unit...}
int : {ProductIdNumeric, Prices_SalesOne, Prices_Treater, Prices_B2C...}
arr : {EnvironmentBrands, Catalogs, NavisionLevels}
long : long
我想在不使用属性名的情况下迭代属性,例如bool
。
我试着像这样索引这个对象:
$document[0]
但这并没有给我带来任何好处,但它也不会导致任何错误。
Select-Object
在某种程度上是可行的,但是我必须使用属性名称,而我不希望这样。
$documents | Select-Object bool,str
ForEach
不迭代属性。
$documents | ForEach {
$_.name
}
返回doc,这是保存bools、int和字符串的标记(XML)的名称。
发布于 2016-06-08 04:29:57
这可以通过使用隐藏属性PSObject
来实现
$documents.PSObject.Properties | ForEach-Object {
$_.Name
$_.Value
}
这不适用于某些PowerShell创建的包含"NoteProperties“(类型为NoteProperty
的属性)的对象(PSObject
)。
See this answer for a method that covers all property types。
发布于 2017-10-06 00:23:30
我更喜欢使用foreach
遍历PowerShell对象:
foreach($object_properties in $obj.PsObject.Properties)
{
# Access the name of the property
$object_properties.Name
# Access the value of the property
$object_properties.Value
}
一般来说,foreach
比Foreach-Object
具有更高的性能。
是的,foreach
实际上不同于幕后的Foreach-Object
。
发布于 2016-06-08 14:31:33
与stej mentioned类似,还有一个带有-MemberType
参数的Get-Member
cmdlet,您可以使用:
$documents | Get-Member -MemberType Property | ForEach-Object {
$_.Name
}
https://stackoverflow.com/questions/37688708
复制相似问题