简体   繁体   English

将一个对象从阵列添加到另一阵列

[英]Add One Object from an Array to another Array

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. 我似乎正在将信息添加到新数组中,但是当我通过执行newarray [0]引用新数组时,它给了我对象的第一项,而不是整个对象本身。

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. csv文件中的数据是计算机主机名,计算机IP地址,错误代码和代理ID。

eg MACHINENAME, 10.10.10.10, ERROR101, 123456FF 例如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
    }
    }
}

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. 结果是由6个元素组成的数组,而不是3个数字和一个数组。

You should use either a [hashtable] or a [PSObject] instead of an array; 您应该使用[hashtable][PSObject]而不是数组; or as you did when you built the original one, you'll need to force it into a one elements array, something like: 或者就像您在构建原始数组时所做的那样,需要将其强制为一个element数组,例如:

$global:MachinesInAD += ,@($global:MachineObject)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM