Hey Y’all,
Now I’ve been programming for decades and thought I had a good grasp of Arrays but PS has me stumped.:angry:
Can someone tell me why I keep getting this error:
Unable to index into an object of type
System.Management.Automation.PSReference`1[System.Object[]].
At line:18 char:8
+ $PathSegment[$SegCnt++] = $Result.value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noparse] + CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex[/noparse]
Here’s the code:
Function Parse-Path { Param ( [Parameter(Mandatory=$True)] [ref]$PathSegment , [Parameter(Mandatory=$True)] [string]$TestPath , [Parameter(Mandatory=$False)] [string]$InclFN = $False ) $SegCnt = 0 $PathRegX = [regex] "[0-9,A-Z,a-z,#,_,-, ]+||:" $Result = $PathRegX.match($TestPath) #$Result | Get-Member While ($Result.Success) { $PathSegment[$SegCnt++] = $Result.value #Stmt causing Error! $Result = $Result.nextmatch() } #End While Loop Return $SegCnt } #End Function Parse-Path #Main Clear-Host $Elements = @(" "," "," "," "," "," "," "," "," "," "," ") $ArrCntr = Parse-Path -PathSegment ([ref]$Elements) ` -TestPath "G:BEKDocsExcelTestSkiPro Example_Revised#2.xlsm" write-host $Elements.GetType() write-host "" Write-Host "There are: $ArrCntr items." For ( $Cnt=1; $Cnt -lt $ArrCntr; $Cnt++) { Write-Host $Elements[$cnt] }
I’ve tried declaring the $Elements array several different ways as well as using different indexing schemes. (This was originally a 2 dimensional array but I cut it back to one for simplicity…and still can’t get it to work.)
Any insight would be most welcome. I’ve googled for 2 days now and can’t find the answer as all the hits show short command line examples rather that actual program segments that you can follow through. :cheers: