Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

PowerShell: Cannot index into a null array

$
0
0

I am trying to put together a script that will upload documents into a document library. Here is what my script looks like currently...

$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

$FileName = $File.Name

$NewName = [IO.Path]::GetFileNameWithoutExtension($FileName)

$FileNameArray = $NewName.split("_")
$check = $FileNameArray.Length

Write-Host "My Array:" $FileNameArray
Write-Host "Array Length:" $check

#Add file
$folder =  $web.getfolder($docLibrary.rootFolder.URL)
write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
$spItem = $spFile.Item

#populate columns
$spItem["First Column"] = $FileNameArray[0].ToString()
$spItem["Second Column"] = $FileNameArray[1].ToString()
$spItem.Update()

$fileStream.Close();
 

Currently when my script tries to update the columns with the elements from the array, I am getting this error message

Cannot index into a null array.
At C:\powershellscripts\Upload-FilesIntoSharePoint.ps1:164 char:6
+                     $spItem["First Column"] = $FileNameArray[0].ToString()
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

...earlier in the script, I placed checks to see what my array is like, and it hold data. When I ran it, I had 2 elements in my array, those values where printed to the console. However, the script stalls when am trying to get the values into the respective columns. Does anyone have an idea why this is happening? Thanks.


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>