Adding arrays to arraylists
Powershell converts ArrayList to Array - Stack Overflow
Convert system.collection arrylist to string array
How to create an ArrayList from an Array in PowerShell? - Stack Overflow
Videos
I am new to Powershell and this is an enhancement to the first script I have written. I have the following Copy item just an example of 1. The script will have about 5 total pointing to different servers Copy-Item -Path \xxx\prod\y.txt -Destination \zzz\test\ -Force -PassThru -ErrorAction "SilentlyContinue" -ErrorVariable +ProcessError -Verbose What I want to do is convert the System.Collections.ArryList ProcessError to a string array that I can include in the body of an email. How do I do this?
I can't get that constructor to work either. This however seems to work:
# $temp = Get-ResourceFiles
$resourceFiles = New-Object System.Collections.ArrayList($null)
$resourceFiles.AddRange($temp)
You can also pass an integer in the constructor to set an initial capacity.
What do you mean when you say you want to enumerate the files? Why can't you just filter the wanted values into a fresh array?
Edit:
It seems that you can use the array constructor like this:
$resourceFiles = New-Object System.Collections.ArrayList(,$someArray)
Note the comma. I believe what is happening is that when you call a .NET method, you always pass parameters as an array. PowerShell unpacks that array and passes it to the method as separate parameters. In this case, we don't want PowerShell to unpack the array; we want to pass the array as a single unit. Now, the comma operator creates arrays. So PowerShell unpacks the array, then we create the array again with the comma operator. I think that is what is going on.
Probably the shortest version:
[System.Collections.ArrayList]$someArray
It is also faster because it does not call relatively expensive New-Object.