Author Posts

March 21, 2018 at 11:27 am

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

March 21, 2018 at 2:14 pm

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.

March 22, 2018 at 12:56 pm

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