Powershell-如何在自定义对象中编辑现有属性 [英] Powershell - how do I edit an existing property in a custom object

查看:64
本文介绍了Powershell-如何在自定义对象中编辑现有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找如何在现有psobject中更新noteproperty的方法,例如,我有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

问题是如何将主页"更新为其他内容.

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

这篇关于Powershell-如何在自定义对象中编辑现有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆