1

I am trying to pass an Item to a SPE Function as follows:

function ImportTask ($importTask, $taskItem) {

    $taskName = $taskItem.Name

    $importTask.ItemTitle = $taskItem.Fields["Item Title"].Value

}

$taskObj = New-Object MyNamespace.Task

$contextItem = Get-Item -Path "."

ImportTask($taskObj, $contextItem)

However, when executed, I get the following error:

Cannot index into a null array.
At line:5 char:2
+     $importTask.ItemTitle = $taskItem.Fields["Item Title"].Value
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Any suggestions why I cannot retrieve the value?

2
  • so what are you trying to do ? assign a field value to $imporTask from $taskItem's field ? Mar 29, 2017 at 15:56
  • I have created an object of type Task, then I am adding a value from the item field Item Title to it. I am doing more processing on this but I am having the above error Mar 29, 2017 at 15:58

1 Answer 1

3

To distinguish positional parameters, Powershell uses spaces instead of commas. By changing your invocation to this, you will fix your problem:

ImportTask $taskObj $contextItem

By using a comma, you are sending an array into the first parameter, and not defining the second.

There are a couple of blog posts by Michael Sorens which detail this in great length:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.