如何通过PowerShell中的属性名称获取对象的属性值?

时间:2013-01-18 19:34:43

标签: powershell

在PowerShell中,如何通过指定对象的名称(字符串)来获取对象的属性值?我想要以下内容:

$obj = get-something

# View the object's members:
$obj | gm

# I could retrieve a property by doing so:
write-host $obj.SomeProp

# But for many purposes, I would really want to:
write-host $obj | Get-PropertyByName "SomeProp"

PowerShell中是否存在类似“Get-PropertyByName”的内容?

6 个答案:

答案 0 :(得分:152)

当然

write-host ($obj | Select -ExpandProperty "SomeProp")

或者就此而言:

$obj."SomeProp"

答案 1 :(得分:35)

扩展@aquinas:

Get-something | select -ExpandProperty PropertyName

Get-something | select -expand PropertyName

Get-something | select -exp PropertyName

我为那些可能正在寻找单行命令以获取某些信息并希望包含一个真实示例的人提出了这些建议。

在通过PowerShell管理Office 365时,以下是我用于获取已添加到“BookInPolicy”列表中的所有用户/组的示例:

Get-CalendarProcessing conferenceroom@domain.com | Select -expand BookInPolicy

仅仅使用“Select BookInPolicy”就会切断几个成员,所以感谢您提供此信息!

答案 2 :(得分:17)

您可以使用Select-Object cmdlet按名称获取属性,并指定您感兴趣的属性名称。请注意,这并非如此只需返回该属性的原始值;相反,你得到的东西仍然像对象一样。

[PS]> $property = (Get-Process)[0] | Select-Object -Property Name

[PS]> $property

Name
----
armsvc

[PS]> $property.GetType().FullName
System.Management.Automation.PSCustomObject

为了对该属性使用,您仍需要确定您追求的属性,即使只有一个属性:

[PS]> $property.Name
armsvc

[PS]> $property -eq "armsvc"
False

[PS]> $property.Name -eq "armsvc"
True

[PS]> $property.Name.GetType().FullName
System.String

根据其他答案,如果要在字符串中使用单个属性,则需要计算表达式(在其周围放括号)和前缀为美元符号($)以将表达式动态声明为变量插入字符串:

[PS]> "The first process in the list is: $($property.Name)"
The first process in the list is: armsvc

非常正确,其他人已通过为-ExpandProperty cmdlet推荐Select-Object参数来回答此问题。这会通过返回指定属性的值来绕过一些令人头疼的问题,但您会希望在不同的场景中使用不同的方法。

  

-ExpandProperty <String>

     

指定要选择的属性,并指示尝试应该   被迫扩大该财产

     

https://technet.microsoft.com/en-us/library/hh849895.aspx

[PS]> (Get-Process)[0] | Select-Object -ExpandProperty Name
armsvc

答案 3 :(得分:4)

试试这个:

$obj = @{
    SomeProp = "Hello"
}

Write-Host "Property Value is $($obj."SomeProp")"

答案 4 :(得分:0)

$com1 = new-object PSobject                                                         #Task1
$com2 = new-object PSobject                                                         #Task1
$com3 = new-object PSobject                                                         #Task1



$com1 | add-member noteproperty -name user -value jindpal                           #Task2
$com1 | add-member noteproperty -name code -value IT01                              #Task2
$com1 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}       #Task3


$com2 | add-member noteproperty -name user -value singh                             #Task2
$com2 | add-member noteproperty -name code -value IT02                              #Task2
$com2 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}       #Task3


$com3 | add-member noteproperty -name user -value dhanoa                             #Task2
$com3 | add-member noteproperty -name code -value IT03                               #Task2
$com3 | add-member scriptmethod ver {[system.Environment]::oSVersion.Version}        #Task3


$arr += $com1, $com2, $com3                                                          #Task4


write-host "windows version of computer1 is: "$com1.ver()                            #Task3
write-host "user name of computer1 is: "$com1.user                                   #Task6
write-host "code of computer1 is: "$com1,code                                        #Task5
write-host "windows version of computer2 is: "$com2.ver()                            #Task3
write-host "user name of computer2 is: "$com2.user                                   #Task6
write-host "windows version of computer3 is: "$com3.ver()                            #Task3
write-host "user name of computer3 is: "$com1.user                                   #Task6
write-host "code of computer3 is: "$com3,code                                        #Task5

read-host

答案 5 :(得分:0)

这里是获取对象属性值的另一种方法:

write-host $(get-something).SomeProp