0

我想向对象数组添加一个新属性。添加会员不起作用,请协助

$computers = Get-ADComputer -Filter {(name -like "SMZ-*-DB")} | select -First 30
workflow test-test
{
    param([Parameter(mandatory=$true)][string[]]$computers)
    $out = @()
    foreach -parallel -throttlelimit 20 ($computer in $computers)
    {
        sequence
        {
            [bool]$ping = (Test-Connection $computer.name -Quiet -Count 1)
            $computer = $computer | Add-Member -NotePropertyName "Ping" -NotePropertyValue $ping            
            $Workflow:out += $computer
            

        }
    }
    return $out  

}
test-test $computers
4

3 回答 3

1

如果您需要对象的所有属性或稍后需要使用该对象,那么将其添加到自定义对象可能更简单:

    sequence
    {
        [bool]$ping = (Test-Connection $computer.name -Quiet -Count 1)
        $newObj = [pscustomobject]@{ADObject = $computer;Ping = $ping}
        $Workflow:out += $newObj
    }

...但通常您只需获取所需的属性:

    sequence
    {
        [bool]$ping = (Test-Connection $computer.name -Quiet -Count 1)
        $newObj = [pscustomobject]@{ComputerName = $Computer.Name;Ping = $ping}
        $Workflow:out += $newObj
    }
于 2020-11-25T13:01:03.157 回答
1

您可以在 powershell 7 中执行此类操作。 Get-ADComputer 中的 ADComputer 对象似乎是只读的,因此您无法向它们添加成员。

Get-ADComputer -filter 'name -like "a*"' -resultsetsize 3 |
foreach-object -parallel {
  $computer = $_
  $ping = Test-Connection $computer.name -Quiet -Count 1
  [pscustomobject]@{
    ADComputer = $computer
    Ping = $ping
  }
}

ADComputer                      Ping
----------                      ----
CN=A001,DC=stackoverflow,DC=com True
CN=A002,DC=stackoverflow,DC=com True
CN=A003,DC=stackoverflow,DC=com True
于 2020-11-25T15:35:57.543 回答
0

您要添加到 Microsoft.ActiveDirectory.Management.ADComputer Typename 而不是数组。

尝试创建一个 PSCustomObject

于 2020-11-25T11:42:58.293 回答