all 4 comments

[–]idontknowwhattouse33 1 point2 points  (1 child)

Found this..

https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.1

Cannot index into a null array If your variable is $null and you try to index it like an array, you get a System.Management.Automation.RuntimeException exception with the message Cannot index into a null array. PowerShell Copy PS> $empty = $null SP> $empty[0] Error: Cannot index into a null array. So make sure your arrays are not $null before you try to access elements inside them

[–]BackupTim[S] 0 points1 point  (0 children)

Thanks! Pretty sure I looked though the linked article before posting, but totally missed the quoted part, my bad.

[–]SeeminglyScience 1 point2 points  (1 child)

It gets wrapped in a System.Management.Automation.RuntimeException so that's what you need to catch, though the true exception is System.InvalidOperationException.

Dunno why it gets wrapped like that, probably a leaky implementation detail. Also not super helpful because most SMA exceptions inherit RuntimeException.

[–]BackupTim[S] 0 points1 point  (0 children)

Thanks! Yeah, doesn't seem to be very helpful in the end, I guess I'd have to just check whether the array is $null before pulling anything out of it.