0

I can't figure out what I'm doing wrong and hope someone can point me in the right direction.

I'm trying to iterate through an array of objects and testing on each object and when something is true, I want to take that object and add it to it's own array, as a single object (just like it was in the original array of objects). I seem to be adding the information to the new array, but when I reference the new array by doing newarray[0] it gives me the first item of the object, not the entire object itself.

The issue appears to be with this line:

$global:MachinesInAD += $global:MachineObject

The data in the csv file is a machine hostname, the machines IP address, an error code, and an agent ID.

e.g. MACHINENAME, 10.10.10.10, ERROR101, 123456FF

Function ReadExcelReport (){
$global:Report = "C:\TEMP\Tools\Scripts\agents.csv"
$Unresponsive = import-csv $global:Report | Where-Object {($_.State -eq "QUEUED" -or $_.State -eq "FAILED")} #Add items to array from spreadsheet where the state is equal to queued or failed
$global:UnresponsiveMachineInfo = @()
foreach ($item in $Unresponsive){
        $global:UnresponsiveMachineInfo += ,@($item.'Hostname', $item.'IP Address',$item.'Error',$item.'Agent Cert ID') #Build the object - Add the following columns hostname, ip address, error, agent cert id
}
ADCheck
}

Function ADCheck (){
$Machine = $null
$global:MachinesInAD = @()
$global:MachinesNotInAD = @()
$global:MachineObject = New-Object system.object
$global:MachineObject = $Null
$global:MachinesInAD = $null

$global:UnresponsiveMachineInfo | foreach-object { #Iterate through each object in the array

    $global:MachineObject = $_
    $Machine = $_[0] #Set Machine to the hostname AKA the first element in the array for the current ($_) object (objects defined above)

    try{ 
        write-host "Checking A.D. for: $Machine"
        if (Get-ADComputer $Machine){ #Check to see if the machine is in A.D.
            write-host "Found $Machine in A.D." -ForegroundColor Green
            $global:MachinesInAD += $global:MachineObject
        }
    }
    catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { #If the machine was NOT in A.D. catch the error it creates and...
        write-warning -message "Machine $Machine not found in A.D."
        $global:MachinesNotInAd += $MachineObject
    }
    }
}
3
  • Can you whittle this down to a minimal reproducible example? You're appending in at least 3 different spots just from glancing at it and it's not clear where you're having the issue. Commented Feb 8, 2017 at 20:00
  • I'll do that, the issue appears to be with this line: "$global:MachinesInAD += $global:MachineObject" Commented Feb 8, 2017 at 20:04
  • I use the "$Machine = $_[0]" to pull out the machine name for testing. I try to capture the element with "$global:MachineObject = $_" and when true, add that element to the new array with "$global:MachinesInAD += $global:MachineObject". Techically, the information gets added, but it doesn't appear to be an individual element. e.g. each item of the element is it's own element. So, when I try to reference the full element with "$global:MachineObject[0]", I only get a hostname, not the hostname, IP, ErrorCode, and agent ID. Commented Feb 8, 2017 at 20:24

1 Answer 1

1

This is happening because what you're calling an object, is just an array (which.. is an object, but your properties are elements, not properties).

Anyway, when you do this:

$global:MachinesInAD += $global:MachineObject

You end up concatenating the arrays.

@(1,2,3) + @(4,5,6)

That results in an array of 6 elements, not 3 numbers and an array.

You should use either a [hashtable] or a [PSObject] instead of an array; or as you did when you built the original one, you'll need to force it into a one elements array, something like:

$global:MachinesInAD += ,@($global:MachineObject)
Sign up to request clarification or add additional context in comments.

1 Comment

While, I admittedly don't quite understand all of this, I'm very grateful. I now know what to study and your solution worked great. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.