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

Welcome Forums General PowerShell Q&A Array elements are wiped out each time when $dataArray += $anObject

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
7 months, 4 weeks ago.

  • Author
    Posts
  • #96611

    Participant
    Points: 0
    Rank: Member

    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)
      If($pObject)
      { 
        $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)
    $dataArray
  • #96629

    Participant
    Points: 59
    Rank: Member

    Please read the section 'Declaring a variable as an array' in this post https://superwidgets.wordpress.com/2018/01/01/practical-guide-to-powershell-arrays/

    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 } 
            [PSCustomObject][Ordered]@{
                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"
        }
    }
    
    $dataArray 
    

    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

    Participant
    Points: 0
    Rank: Member

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

The topic ‘Array elements are wiped out each time when $dataArray += $anObject’ is closed to new replies.