在 PowerShell 中迭代 PSObject 属性 [英] Iterate over PSObject properties in PowerShell
本文介绍了在 PowerShell 中迭代 PSObject 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 PSObject
(来自 XML):
I have this PSObject
(from 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
.
I would like to iterate over the properties without using the property names, for instance bool
.
我尝试像这样索引对象:
I have tried to index into the object like this:
$document[0]
但这并没有给我什么,但它也不会导致任何错误.
But that gives me nothing, but it does not cause any errors either.
Select-Object
有点工作,但是我必须使用属性名称,我不想要那样.
Select-Object
kind of works, but then I have to use the property names, and I don't want that.
$documents | Select-Object bool,str
ForEach
不迭代属性.
$documents | ForEach {
$_.name
}
返回 doc,这是包含布尔值、整数和字符串的标记 (XML) 的名称.
returns doc and that is the name of the tag (XML) holding the bools, ints, and strings.
推荐答案
这可以使用隐藏属性 PSObject
:
This is possible using the hidden property PSObject
:
$documents.PSObject.Properties | ForEach-Object {
$_.Name
$_.Value
}
这篇关于在 PowerShell 中迭代 PSObject 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文