I am used to coding in java and I am brand new to Powershell. I have a text file that contains Windows server info displayed like this.
14.0.3026.27,None,CU7,4229789,SQL Server 2017,0 14.0.3025.34,None,CU6,4101464,SQL Server 2017,0 14.0.3023.8,None,CU5,4092643,SQL Server 2017,0
I am trying to throw this info into a 2 dimensional array and want it to look like this.
[14.0.3026.27],[None],[CU7],[4229789],[SQL Server 2017],[0] [14.0.3025.34],[None],[CU6],[4101464],[SQL Server 2017],[0] [14.0.3023.8],[None],[CU5],[4092643],[SQL Server 2017],[0]
The code I have is giving this error message:
Cannot index into a null array. At line:9 char:9 + $array[$i][$j] = $word + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
Here is my code:
$file = Get-Content "C:\Users\UserName\Desktop\sqlServers.txt"
$array = @(), @()
$i = 0
$j = 0
foreach ($line in $file){
$j=0
foreach ($word in $line.split(",")){
$array[$i][$j] = $word
$j+=1
}
$i+=1
}