Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

powershell - Strange difference between $_ and [parameter(ValueFromPipeline=$true)]

I've come to the edge of my PoweShell-fu. Can someone explain to me why these two functions act differently when piping an array of arrays? All that's different is whether I'm using $_ or [parameter(ValueFromPipeline=$true)] $input to get at the piped input. I expected those to act identically in this situation.

$pairs = ('a', 'b'), ('c', 'd')

function dollarUnderscoreFunction
{
    Process
    {
        Write-Host "`$_[0] = $($_[0])"
        Write-Host "`$_[1] = $($_[1])"
    }
}

function pipedParameterFunction([parameter(ValueFromPipeline=$true)] $input)
{
    Process
    {
        Write-Host "`$input[0] = $($input[0])"
        Write-Host "`$input[1] = $($input[1])"
    }
}

Write-Host "`$pairs:"
$pairs | foreach { Write-Host $_ }

Write-Host "`nRunning dollarUnderscoreFunction`n"
$pairs | dollarUnderscoreFunction

Write-Host "`nRunning pipedParameterFunction`n"
$pairs | pipedParameterFunction

Output in PowerShell v3:

$pairs:
a b
c d

Running dollarUnderscoreFunction

$_[0] = a
$_[1] = b
$_[0] = c
$_[1] = d

Running pipedParameterFunction

$input[0] = a b
$input[1] =
$input[0] = c d
$input[1] =

Output in PowerShell v2:

$pairs:
a b
c d

Running dollarUnderscoreFunction

$_[0] = a
$_[1] = b
$_[0] = c
$_[1] = d

Running pipedParameterFunction

[ : Unable to index into an object of type System.Collections.ArrayList+ArrayListEnumeratorSimple.
At C:Untitled1.ps1:16 char:8
+ $input[ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

$input[0] =
[ : Unable to index into an object of type System.Collections.ArrayList+ArrayListEnumeratorSimple.
At C:Untitled1.ps1:17 char:8
+ $input[ <<<< 1]
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

$input[1] =
[ : Unable to index into an object of type System.Collections.ArrayList+ArrayListEnumeratorSimple.
At C:Untitled1.ps1:16 char:8
+ $input[ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

$input[0] =
[ : Unable to index into an object of type System.Collections.ArrayList+ArrayListEnumeratorSimple.
At C:Untitled1.ps1:17 char:8
+ $input[ <<<< 1]
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

$input[1] =
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As per my comment $input is a reserved automatic variable. If you change it in your pipedparameterfunction with another named variable you will have the expected behaviour.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...