Tim Lovell-Smith
Tim Lovell-Smith

Reputation: 16115

How can PowerShell foreach possibly be failing with a null array in this code?

How can this powershell code possibly fail with 'Cannot index into a null array.'? It makes no sense to me at all. Surely $cloudPfx should be a non-null array?

$cloudPfx = @( "Foo.pfx", "Bar.pfx", $SSLCertFileName )
$cloudPfx | foreach {
    ## call some function...
    Save-Pfx "Foo\$_" (Join-Path $SomePath "Bar$_")
}

foreach : Cannot index into a null array.
At [that line]
+     $cloudPfx | foreach {
+                 ~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
+ FullyQualifiedErrorId : NullArray,Microsoft.PowerShell.Commands.ForEachObjectCommand

Upvotes: 1

Views: 1620

Answers (1)

Tim Lovell-Smith
Tim Lovell-Smith

Reputation: 16115

So, by adding debug output statements - it turns out that the problem is really occurring inside the Save-Pfx function. It's just that the error presentation is completely wrong. (I think Roman probably suspected this, given that he suggested switching to foreach statements.)

Upvotes: 1

Related Questions