Git Product home page Git Product logo

Comments (5)

mklement0 avatar mklement0 commented on June 12, 2024 1
  • $_ only has a meaningful value with pipeline input, not also when input is provided via arguments.

  • Fundamentally, do not rely on $_ in the process block of advanced script functions / scripts that explicitly declare pipeline-binding parameters - only use the latter.

In your case, this means that your process block should (a) only act on $Assertions, and (b) be aware of the fact that $Assertions, due to its declaration as [hashtable[]], is always an array of [hashtable] instances, even if [hashtable] instances are provided one by one via the pipeline.

from powershell.

SetTrend avatar SetTrend commented on June 12, 2024 1

Thanks a lot for enlightening me, @mklement0!

Would you agree that this important fact should be mentioned in the docs at
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_methods#process

Describes how functions that specify the `CmdletBinding` attribute can use the methods and properties that are available to compiled cmdlets.

PS: Yes I'm intentionally using [hashtable[]], so I don't need to make this harmonization (if ($Assertions -isnot [array]) $Assertions = @($Assertions)) in my code.

from powershell.

mklement0 avatar mklement0 commented on June 12, 2024 1

Glad to hear it helped, @SetTrend , and I agree re the docs; see:

from powershell.

sdwheeler avatar sdwheeler commented on June 12, 2024

The documentation for the process block says:

The automatic variable $_ or $PSItem contains the current object in the pipeline for use in the process block.

Since you are calling the function with a parameter, there is nothing in the pipeline. The function needs to handle the parameter variable. This version of the function works for both pipeline input and parameter input.

function Test-Something {
    param (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [hashtable[]]$Assertions
    )

    begin { [hashtable[]]$retValue = @{} }

    process {
        foreach ($assertion in $Assertions) {
            $retValue += $assertion
        }
    }

    end { $retValue }
}

from powershell.

mklement0 avatar mklement0 commented on June 12, 2024

For context, @sdwheeler:

  • This previous comment already explains the issue, and it makes it clear that you shouldn't use $_ in the process block of advanced functions - use the parameter (variable) declared to be pipeline-binding.

  • @SetTrend had run into the original problem because had relied on the documentation, which confusingly actively suggests using $_ (which is only appropriate for non-advanced functions).

    • The fact that the passage you quote from the documentation only mentions the pipeline is technically correct, but doesn't prevent conceptual confusion, as evidenced by the case at hand.
  • He then suggested that the documentation be amended, causing me to create MicrosoftDocs/PowerShell-Docs#10944, which you've recently closed as resolution-wont-fix, which is inappropriate in my estimation.

from powershell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.