Powershell 向对象数组添加新属性。平行的

Powershell 向对象数组添加新属性。平行的,powershell,powershell-workflow,Powershell,Powershell Workflow,我想向对象数组添加一个新属性。添加成员无效,请协助 $computers = Get-ADComputer -Filter {(name -like "SMZ-*-DB")} | select -First 30 workflow test-test { param([Parameter(mandatory=$true)][string[]]$computers) $out = @() foreach -parallel -throttlelimit

我想向对象数组添加一个新属性。添加成员无效,请协助

$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

您正在向Microsoft.ActiveDirectory.Management.ADComputer Typename添加的不是数组


尝试创建PSCustomObject,而不是将其添加到Microsoft.ActiveDirectory.Management.ADComputer Typename而不是数组中


如果需要对象的所有属性或以后需要使用该对象,请尝试创建PSCustomObject,然后将其添加到自定义对象中可能更简单:

    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
    }

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

    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
    }

您可以在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
  }
}


您可以在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
  }
}


@a有没有办法将Microsoft.ActiveDirectory.Management.ADComputer复制到自定义对象?js2010提供了完整的解决方案,请在确认正确后标记答案working@a有没有办法将Microsoft.ActiveDirectory.Management.ADComputer复制到自定义对象?js2010提供了完整的解决方案,请在确认有效后标记答案