user2978216
user2978216

Reputation: 568

Adding a new property to an array of objects. Parallel

I'd like to add a new property to an array of objects. Add-member doesn't work, please assist

$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

Upvotes: 0

Views: 330

Answers (3)

js2010
js2010

Reputation: 27428

You can do something like this in powershell 7. It looks like the ADComputer objects from Get-ADComputer are read-only, so you can't add members to them.

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

Upvotes: 1

Scepticalist
Scepticalist

Reputation: 3923

If you need ALL the properties of the object or need to use the object later then probably simpler to add it once to the custom object:

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

...but usually you'd just grab the properties you need:

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

Upvotes: 1

A Seyam
A Seyam

Reputation: 347

You are adding to Microsoft.ActiveDirectory.Management.ADComputer Typename not an array.

Try to create a PSCustomObject instead

Upvotes: 0

Related Questions