1

我已经创建了一个返回自定义 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
4

1 回答 1

2

(我的评论作为答案)

$Object += __$Objects = $Objects + __;的缩写

所以这一行:

$Objects += New-Object psobject -Property $ObjectProperties

期望$Objects成为一个列表,然后将一个项目添加到列表中。你还没有说它是否应该是一个列表,所以我猜你只想制作一个对象,并且应该使用=而不是+=

$Objects = New-Object psobject -Property $ObjectProperties

然后你可以这样做:

$Objects.Computer
于 2015-11-03T06:18:02.960 回答