使用Add函数的Powershell系统阵列



我正在从EventViewer中提取一些数据,它将输出返回为system.array:

$GetEvents = [regex]::Split((Get-EventLog -source $source -LogName $logname -EntryType Warning -InstanceId $id -Newest 1).Message, 'n')

输出可以是这样的:

10:40

55:3.4

我正在尝试使用Add功能,但我一直收到以下错误:

使用"1"参数调用"Add"时发生异常:"集合的大小固定。">

这就是我尝试使用"添加"功能的方式:

foreach($item in $array)
{
$GetEvents.Add($item)
}

$array也是基于system.array 的类型

我知道我可以做以下事情:

$GetEvents = $array

但这并不是我想要实现的,我将数组中的项目与$GetEvents中的项目进行比较。

例如:

$array = '22: 15','35: 4','10: 40'
$GetEvents = '10: 40','55: 3.4'

$数组中$GetEvents中不存在的每个"项"都需要添加到$GetEvents。

CCD_ 1也不会起作用。

CCD_ 2也将从该阵列中移除数据。

我希望我的解释很清楚,这是可以理解的。

提前感谢!

System.Array的实际上不可调整大小,Add()方法仅用于满足IList接口。

您可能需要使用实际列表:

$GetEvents = [System.Collections.Generic.List[string]]::new()
$GetEvents.AddRange(
[regex]::Split((Get-EventLog -source $source -LogName $logname -EntryType Warning -InstanceId $id -Newest 1).Message, 'n')
)
foreach($item in $array){
$GetEvents.Add($item)
}

最新更新