1

I'm pulling some data from my EventViewer which returns the output as system.array:

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

the output can look like this:

10: 40

55: 3.4

I'm trying to use the Add function but i keep getting the following error:

Exception calling "Add" with "1" argument(s): "collection was of a fixed size."

this is how i'm trying to use the "Add" function:

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

the $array is also based type as system.array

i know i can do the following:

$GetEvents = $array

but thats not what im trying to achieve, im comparing between the items i have in the array to the ones i have inside my $GetEvents.

for example:

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

Every "item" that doesnt exist in the $GetEvents from $array needs to be added to $GetEvents.

also the $GetEvent += $item wont do the trick.

also $GetEvents = New-Object System.Collections.ArrayList will remove the data from this array.

I hope i was clear in my explanation and it's understandable.

Thanks in advance!

Current Redemption
  • 169
  • 1
  • 1
  • 11
  • 1
    what are the _specific values_ used in your `Get-EventLog` call? i cannot reproduce your problem with incomplete info ... [*grin*] PLEASE, add that info to your Question & wrap it in code formatting. – Lee_Dailey Apr 21 '20 at 06:10
  • @Lee_Dailey Not sure i understood what you asked? specific values? i wrote it on the top of the question: $GetEvents = [regex]::Split((Get-EventLog -source $source -LogName $logname -EntryType Warning -InstanceId $id -Newest 1).Message, '\n') $source can be anything $logname can be application $id can be 123 is that was you reffering to? – Current Redemption Apr 21 '20 at 06:16
  • 2
    Both the addition operator (`+=`) and the `.Add(...)` method require you to initiate the array (or ArrayList) outside (prior) the `ForEach` loop. In any case, both syntaxes are probably not desired, instead you should try to use the PowerShell pipeline to build a new collection, see: [Why should I avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/q/60708578/1701026) – iRon Apr 21 '20 at 06:37
  • @CurrentRedemption - i am asking what is in the $Vars that you show ...but don't define. i cannot test your code since you did not tell anyone what is in `$Source`, `$LogName`, or `$Id` ... [*grin*] – Lee_Dailey Apr 21 '20 at 10:29
  • You're using the `-Newest` flag with a value of 1, Get-EventLog will return a single EventLogEntry object rather than an array. Is that correct? – Nick Graham Apr 21 '20 at 12:18

1 Answers1

1

System.Array's are not actually resizable, the Add() method is only exposed to satisfy the IList interface.

You might want to use an actual list instead:

$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)
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206