Array elements are wiped out each time when $dataArray += $anObject

This topic contains 2 replies, has 2 voices, and was last updated by  Gnart 2 months ago.

  • Author
  • #96611


    I am working on a simple script to learn PowerShell. The script code follows. The basic issue is:
    (a) An $dataArray = @() is defined.
    (b) An $dataObject (PSObject) is created and populated with data
    (c) code line is intended to store data: $dataArray += $dataObject
    Problem: The code line executed. 1st time line is executed, $dataObject content is stored in $dataArray[0]. 2nd time line is executed, new $dataObject is stored in $dataArray[0] and repeat $dataArray[1]. 3rd time line is executed, new $dataObject is stored in $dataArray[0] and repeat $dataArray[1] and $dataArray[2]. The end result is that the content of the last $dataObject filled the array. All previous data was lost.
    Objective: My objective is to maintain the integrity of the $dataArray to dump it at the end.
    The Code:

    $dataArray = @() # Array to store data
    $outputProperty = @{
      Hostname = $Null
      IPv4 = $Null
      IPv6 = $Null
      MAC = $Null
      MACDesc = $Null
    $dataObject = New-Object -TypeName PSObject -Property $outputProperty
    $Computers = Get-ADComputer -filter * | select -expandproperty name
    Foreach($Machine in $Computers)
      $pObject = ''
      $pObject = (Test-Connection -Cn $Machine -BufferSize 16 -Count 1)
        $colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $Machine
        foreach ($objItem in $colItems) 
          if($objItem.DefaultIPGateway -gt "") 
            $dataObject.MACDesc = $objItem.Description
            $dataObject.MAC     = $objItem.MACAddress
        } # foreach ($objItem in $colItems) 
        $dataObject.Hostname = $pObject.address
        [string]$dataObject.IPv4 = $pObject.IPv4address -split [char]13
        [string]$dataObject.IPv6 = $pObject.IPv6address -split [char]13
        $dataArray += $dataObject
      } # If($pObject)
    } # Foreach($Machine in $Computers)
  • #96629

    Sam Boutros

    Please read the section 'Declaring a variable as an array' in this post

    Try this:

    $ComputerList = Get-ADComputer -filter * | select -expandproperty name
    $dataArray = foreach ($ComputerName in $ComputerList) {
        if (Test-Connection -ComputerName $ComputerName -BufferSize 16 -Count 2 -EA 0) {
            $IPInfo = Get-WmiObject -Class 'Win32_NetworkAdapterConfiguration' -ComputerName $ComputerName | where { $PSItem.DefaultIPGateway } 
                Hostname = $ComputerName
                IPv4     = ($IPInfo.IPAddress -match '\.')
                IPv6     = ($IPInfo.IPAddress -notmatch '\.')
                MAC      = $IPInfo.MACAddress
                MACDesc  = $IPInfo.Description
        } else {
            Write-Host "Computer '$ComputerName' is offline or cannot be reached"

    In the Test-Connection (ping) command you want at least 2 packets. First packet may not get there if the connecting switch is building its MAC table.

    I picked up the IP addresses from the GWMI returned object instead of the Test-Connection returned object.

  • #96749


    Thanks Sam B. for the reading suggestion on array. I found the answer from a post in a TechNet post.

You must be logged in to reply to this topic.