【问题标题】:Can't Remove Item from Array无法从数组中删除项目
【发布时间】:2015-05-22 23:03:15
【问题描述】:

我正在尝试从数组中删除项目,但无论我使用什么方法,它似乎都不起作用。我需要一些帮助来了解这里发生的事情。

我通常默认使用System.Array,但这不起作用,我now understand why

[array]$arrADComputers = Get-ADComputer -Filter 'OperatingSystem -like "Windows 7 Enterprise" ' -Properties Name | select name
$arrADComputers.GetType()
$arrADComputers.count
$arrADComputers.Remove("Machine")
$arrADComputers.count

所以我尝试使用System.Collections.ObjectModel.Collection,但这也不起作用。

$Collection1 = {$arrADComputers}.Invoke()
$Collection1.GetType()
$Collection1.Count
$Collection1.Remove("Machine")
$Collection1.count

System.Collections.ArrayList也是如此

[System.Collections.ArrayList]$Collection2 = $arrADComputers
$Collection2.GetType()
$Collection2.Count
$Collection2.Remove("Machine")
$Collection2.count

但是如果我像这样创建一个数组,它可以正常工作:

[System.Collections.ArrayList]$Trash1 = 1,"this",2,"that",3,"them","4"
$Trash1.GetType()
$Trash1.Count
$Trash1.Remove("that")
$Trash1.Count

$TrashTmp = 1,"this",2,"that",3,"them","4"
$Trash2 = {$TrashTmp}.Invoke()
$Trash2.GetType()
$Trash2.Count
$Trash2.Remove("that")
$Trash2.Count

我没有理解哪些概念会阻止我从数组中删除项目?

【问题讨论】:

    标签: .net arrays powershell


    【解决方案1】:

    Get-ADComputer 的返回类型是 [Microsoft.ActiveDirectory.Management.ADComputer],而不是 [String],因此您不能只通过提供计算机名称来删除计算机。

    您可以遍历集合并在其 name 属性匹配时将其删除,但您最好先使用 Where-ObjectGet-ADComputer-Filter 参数进行过滤。

    如果您只关心计算机名称,则仅使用名称创建数组列表:

    $Collection = [System.Collections.ArrayList](Get-ADComputer -Filter * | Select-Object -ExpandProperty Name)
    $Collection.Remove('ComputerName')
    

    这应该可行,因为现在 arraylist 是所有字符串(只是名称)。

    【讨论】:

    • 如果需要进一步处理 ADComputer 对象,可以使用以计算机名称作为键的哈希表来代替 ArrayList
    • 感谢您的解释和解决方案。这更有意义,现在它按预期工作。 [System.Collections.ArrayList]$arrADComputers = Get-ADComputer -Filter 'OperatingSystem -like "Windows 7 Enterprise"' | Select-Object -ExpandProperty Name;$arrADComputers.Count;$arrADComputers.Remove("machine1");$arrADComputers.Count 再次感谢!
    【解决方案2】:

    @briantist 在这里给出了实际问题,因为您没有字符串数组,但您有一个 ADComputer 对象数组。因此,当您尝试删除字符串“machine”时,它找不到。一个有点棘手的解决方法是重建您的数组,不包括目标对象,以删除一个 ADComputer 对象。像这样的:

    [array]$arrADComputers = Get-ADComputer -Filter 'OperatingSystem -like "Windows 7 Enterprise" ' -Properties Name | select name
    $arrADComputers.GetType()
    $arrADComputers.count
    $arrADComputers = $arrADComputers | Where{$_.samaccountname -ne "machine"}
    $arrADComputers.count
    

    或者,您可以找到要删除的对象,然后像以前一样使用Remove() 方法,例如:

    [array]$arrADComputers = Get-ADComputer -Filter 'OperatingSystem -like "Windows 7 Enterprise" ' -Properties Name | select name
    $arrADComputers.GetType()
    $arrADComputers.count
    $objComputer = $arrADComputers | Where{$_.samaccountname -eq "machine"}
    $arrADComputers.Remove($objComputer)
    $arrADComputers.count
    

    【讨论】:

    • 非常感谢您的解释和骇人听闻的解决方法:) 我曾考虑过这样做,但在我看来,我只是在解决知识上的差距。尽管如此,仍然有效!再次,我非常感谢您的解释。
    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-10-19
    • 2020-01-05
    • 1970-01-01
    • 2021-10-03
    • 2014-09-21
    相关资源
    最近更新 更多