创建引用其他对象的自定义对象
Create a custom object referencing other objects
我正在尝试使用来自另一个对象的数据输入创建一个新的自定义对象。
$clusters = Get-EMRClusters
$runningclusters = $clusters | Where-Object {
$_.Status.State -eq "Running" -or
$_.Status.State -eq "Waiting"
}
$runningclusters
看起来像
id name status
-- ---- ------
j-12345 cluster1 running
j-4567 cluster2 running
我想创建一个新的 PSobject $o
,其中第 4 列名为 PendingShutdown
,这是一个布尔值。
id name status pendingshutdown
-- ---- ------ ---------------
j-12345 cluster1 running False
j-4567 cluster2 running False
我试过运行这个:
$o = New-Object PSObject
$o | Add-Member -NotePropertyName id -NotePropertyValue $runningclusters.id
$o | Add-Member -NotePropertyName name -NotePropertyValue $runningclusters.name
$o | Add-Member -NotePropertyName status -NotePropertyValue $runningclusters.status.state
$o | Add-Member -NotePropertyName PendingShutdown -NotePropertyValue $true
但是我对 $o
的 id
和 name
列的输出只是对象本身,而不是 ID 行。如何使对象看起来像上面我想要的对象?
您需要遍历每个集群对象。您可以遍历它们并将列添加到当前对象,例如:
$runningclusters = $clusters |
Where-Object {$_.Status.State -eq "Running" -or $_.Status.State -eq "Waiting"} |
Add-Member -NotePropertyName pendingshutdown -NotePropertyValue $true -PassThru
或者您可以为每个集群创建新对象。例如:
$MyNewClusterObjects = $runningclusters | ForEach-Object {
New-Object -TypeName psobject -Property @{
id = $_.id
name = $_.name
status = $_.status.state
PendingShutdown = $true
}
}
只需使用 calculated properties 即可将属性添加到管道中的对象,例如像这样:
$runningclusters = $clusters | Where-Object {
$_.Status.State -eq "Running" -or
$_.Status.State -eq "Waiting"
} | Select-Object *,@{n='PendingShutdown';e={$false}}
我正在尝试使用来自另一个对象的数据输入创建一个新的自定义对象。
$clusters = Get-EMRClusters
$runningclusters = $clusters | Where-Object {
$_.Status.State -eq "Running" -or
$_.Status.State -eq "Waiting"
}
$runningclusters
看起来像
id name status -- ---- ------ j-12345 cluster1 running j-4567 cluster2 running
我想创建一个新的 PSobject $o
,其中第 4 列名为 PendingShutdown
,这是一个布尔值。
id name status pendingshutdown -- ---- ------ --------------- j-12345 cluster1 running False j-4567 cluster2 running False
我试过运行这个:
$o = New-Object PSObject
$o | Add-Member -NotePropertyName id -NotePropertyValue $runningclusters.id
$o | Add-Member -NotePropertyName name -NotePropertyValue $runningclusters.name
$o | Add-Member -NotePropertyName status -NotePropertyValue $runningclusters.status.state
$o | Add-Member -NotePropertyName PendingShutdown -NotePropertyValue $true
但是我对 $o
的 id
和 name
列的输出只是对象本身,而不是 ID 行。如何使对象看起来像上面我想要的对象?
您需要遍历每个集群对象。您可以遍历它们并将列添加到当前对象,例如:
$runningclusters = $clusters |
Where-Object {$_.Status.State -eq "Running" -or $_.Status.State -eq "Waiting"} |
Add-Member -NotePropertyName pendingshutdown -NotePropertyValue $true -PassThru
或者您可以为每个集群创建新对象。例如:
$MyNewClusterObjects = $runningclusters | ForEach-Object {
New-Object -TypeName psobject -Property @{
id = $_.id
name = $_.name
status = $_.status.state
PendingShutdown = $true
}
}
只需使用 calculated properties 即可将属性添加到管道中的对象,例如像这样:
$runningclusters = $clusters | Where-Object {
$_.Status.State -eq "Running" -or
$_.Status.State -eq "Waiting"
} | Select-Object *,@{n='PendingShutdown';e={$false}}