Powershell-如何在自定义对象中编辑现有属性

问题描述:

我在寻找如何更新现有psobject的音符属性的方法,例如,我有psobjects($ a)的system.array:

I looking for way how update noteproperty in existing psobject, for example I have system.array of psobjects ($a):

Group                                Assigment
-----                                ---------
Group1                               Home
Group2                               Office

问题是如何将 Home更新为其他内容。

Question is how update 'Home' to something other.

$ a | gm:

TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Assigment   NoteProperty System.String Assigment=Office
Group       NoteProperty System.String Group=Group1

$ a.GetType():

$a.GetType():

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

感谢您的未来帮助。

目前还不清楚您的问题是什么:选择好的对象或更新其值?

It's not really clear what is your problem : select the good object or update it's value ?

$col=@() 
$props=@{"group"="group1";"assignment"="home"}
$col += new-object pscustomobject -property $props
$props2=@{"group"="group2";"assignment"="office"}
$col += new-object pscustomobject -property $props2

#select object with home assignment
$rec=$col | where {$_.assignment -eq "home"}
#replace the value
$rec.assignment="elsewhere"

#display collection with updated value
$col