我已经创建了一个返回自定义psobject 的函数,但是当我尝试使用“。”来获取对象注释的属性时,也是管道不在这种类型的物体中工作。
有一种方法可以将此对象转换为使用cmdlet New-object创建的对象吗?
为什么新的psobject缺少功能?
$ObjectProperties = @{
Users = "admin"
Computer = "localhost"
Error = $false
}
$Objects += New-Object psobject -Property $ObjectProperties
# I am trying to use select this object like $objects.computer but doesnt work
$Objects.
$objectTest = New-Object Object
$objectTest | Add-Member NoteProperty error $false
$objectTest | Add-Member NoteProperty Computadores "localhost"
$objectTest | Add-Member NoteProperty Usuarios "admin"
#I need an object like this that can be easy managed
$objectTest
我尝试过以这种方式转换它但不起作用
$objectCompu = $objects |select computer|Out-String
$objectError = $Objects |select error|Out-String
$objectUsers = $Objects |select users |Out-String
$objectTest = New-Object Object
$objectTest | Add-Member NoteProperty error $objectError
$objectTest | Add-Member NoteProperty Computadores $objectCompu
$objectTest | Add-Member NoteProperty Usuarios $objectUsers
$objectTest
答案 0 :(得分:2)
(我的评论作为答案)
$Object += __
是$Objects = $Objects + __
;
所以这一行:
$Objects += New-Object psobject -Property $ObjectProperties
期望$Objects
成为列表,然后将项添加到列表中。你还没有说它是否应该是一个列表,所以我想你只想创建一个对象,并且应该使用=
而不是+=
:
$Objects = New-Object psobject -Property $ObjectProperties
然后你可以这样做:
$Objects.Computer