PowerShell解析数组列表 问题

PowerShell解析数组列表 问题,powershell,arraylist,Powershell,Arraylist,我正在编写一个使用多个ArrayList对象的脚本,这些对象的内容通过过滤器放置,结果存储到临时ArrayList中。然而,我总是遇到这样一个问题:我得到的数据太多,我并不期望,也不知道它来自哪里 示例对象: $installInfos = New-Object System.Collections.ArrayList $InstallInfo = New-Object PSObject -Property @{ SoftwareName = "Some Name" Servic

我正在编写一个使用多个ArrayList对象的脚本,这些对象的内容通过过滤器放置,结果存储到临时ArrayList中。然而,我总是遇到这样一个问题:我得到的数据太多,我并不期望,也不知道它来自哪里

示例对象:

$installInfos = New-Object System.Collections.ArrayList
$InstallInfo = New-Object PSObject -Property @{
    SoftwareName = "Some Name"
    ServiceName = "Some Service"
    ProcessName = "Some Process"
    FileLocation = "Some Directory"
    StartupType = "Auto"
}
$installInfos.Add($installInfo)
这些对象存储在它们自己的.psm1文件中,并按如下方式调用:

Import-Module (Join-Path $PSScriptRoot ""InstallerInfo.psm1") -Force | Out-Null
$tempServicesList = ReturnAllData #This is a function inside the psm1 file that returns the $installInfos ArrayList
接下来。。。使用表单复选框,我解析数据一次,以便仅与用户选择的服务交互:

$checkedServices = New-Object System.Collections.ArrayList

ForEach ($item in $SoftwareCheckboxes) {
    if ($item.Checked) {
        if (item.Text) {
            $checkedServices.Add($item.Text)
        }
    }
}

$selectedServices = New-Object System.Collections.ArrayList
ForEach ($service in $tempServicesList) {
    ForEach ($checkedService in $checkedServices) {
        If ($checkedService -eq $service.SoftwareName) {
            $selectedServices.Add($service)
        }
    }
}
到目前为止,一切都很好。甚至内容也如预期的那样。。。然而,在下一部分中,我无法解释正在发生的脱节

我在一个函数
getSelectedServices
中包含了上述大部分内容,该函数的最后一行是
return$selectedService
。然后,我创建一个新对象,该对象调用该函数来填充自身,如下所示:

$selectedServicesList = getSelectedServices
然而,我一直有问题。。。经过几个小时的调试,我发现问题的症结在于
$selectedServicesList
中存储了哪些数据。我得到的不是一个只包含存储在
$selectedServices
中的对象的新ArrayList对象,而是一系列数字,占据了几个起始索引。在某些情况下,它只会达到索引15~16,在其他情况下,它会达到索引83。这些值最终看起来像:

Index 78 = 37
Index 79 = 38
Index 80 = 39
Index 81 = 40
Index 82 = @{<#objects data#>}
Index 83 = @{<#objects data#>}
Index 84 = @{<#objects data#>}
索引78=37
指数79=38
指数80=39
指数81=40
索引82=@{}
索引83=@{}
索引84=@{}
问题:
有人知道这是什么原因吗?我如何修复它?

ArrayList.Add返回所添加项的索引,并将其放入输出流

要防止此副作用,请使用此模式:

$null = $checkedServices.Add($item.Text)
-or-
$checkedServices.Add($item.Text) | Out-Null

您的代码中是否有额外的双引号<代码>导入模块(加入路径$PSScriptRoot“InstallerInfo.psm1”)不,这是一个意外,因为我是手工重新键入的,而不是复制粘贴(加上省略了我无法放在公共站点上的部分)啊,谢谢。这帮了大忙