2
votes

I am trying to find the index of an element from an arraylist using powershell script,however getting the below given error

Method invocation failed because [System.String[]] doesn't contain a method named 'IndexOf'.

Code Used:

[String[]]$eventList = New-Object System.Collections.ArrayList
$eventList.GetType().FullName 
$index = $eventList.IndexOf('cvv');
1
What is 'cvv' in this context?Benjamin Hubbard
Sadly, I don't think powershell arrays support IndexOf.lurker
'cvv' is an string that i m trying to find in $eventList which is null the thing is i want the location of 'cvv' in $eventList null it should return '-1' but it is returning errorLovepreet Singh

1 Answers

1
votes

You are casting the ArrayList to an array, just remove the cast:

$eventList = New-Object System.Collections.ArrayList

Also, you might want to consider using a List<string> instead of an ArrayList:

$eventList = New-Object System.Collections.Generic.List[string]