Delay-Bind: Is it possible to prevent binding unless a scriptblock is used?

Welcome Forums General PowerShell Q&A Delay-Bind: Is it possible to prevent binding unless a scriptblock is used?

Viewing 5 reply threads
  • Author
    Posts
    • #238727
      Participant
      Topics: 5
      Replies: 12
      Points: 119
      Rank: Participant

      I want to be able to pipe from Function A to Function B, but I only want Function B to bind to properties if the parameter is named or is named and a script block is used when calling the function. The effect would be that only parameters that are explicitly called bind, even if present on the object passed by Function A.

      • This topic was modified 1 week, 2 days ago by Phatmandrake.
      • This topic was modified 1 week, 2 days ago by Phatmandrake.
    • #238736
      js
      Participant
      Topics: 30
      Replies: 828
      Points: 2,556
      Helping Hand
      Rank: Community Hero

      Can you give an example?

    • #238742
      Participant
      Topics: 5
      Replies: 12
      Points: 119
      Rank: Participant
      Function A-Function {
      
      [PSCustomObject]@{
      Param1="ValueA"
      Param2="ValueB"
      Param3="ValueC"
      }
      
      }
      
      Function B-Function {
      
      param(
      
      [Parameter(ValueFromPipelineByPropertyName)]
      [string]
      $Param1,
      [Parameter(ValueFromPipelineByPropertyName)]
      [string]
      $Param2,
      [Parameter(ValueFromPipelineByPropertyName)]
      [string]
      $Param3
      
      )
      
      $Param1
      $Param2
      $Param3
      
      }
      
      A-Function | B-Function -Param1 ValueX
      
      Output:
      
      ValueX             
      ValueB                                                 
      ValueC

      Would there be a way to prevent the parameter binding from happening on param2 and param3? It inherently binds, is there a way to prevent it?

    • #238745
      js
      Participant
      Topics: 30
      Replies: 828
      Points: 2,556
      Helping Hand
      Rank: Community Hero

      I get the error: Missing an argument for parameter ‘Param1’.

    • #238961
      Participant
      Topics: 5
      Replies: 12
      Points: 119
      Rank: Participant

      Just supply a value to Param1, it will output as indicated. Is there a way to prevent parameter binding is the nature of the question though.

    • #238976
      js
      Participant
      Topics: 30
      Replies: 828
      Points: 2,556
      Helping Hand
      Rank: Community Hero

      That’s sort of like saying, “how can I ignore the parameters that I’m sending?” Why would you want to? You could use select-object to only send the one property.

      B-Function -Param1 ValueA -Param2 ValueB -Param3 Valuec
      
      A-Function | select param1 | B-Function 
      • This reply was modified 1 week, 1 day ago by js.
      • This reply was modified 1 week, 1 day ago by js.
      • This reply was modified 1 week, 1 day ago by js.
Viewing 5 reply threads
  • You must be logged in to reply to this topic.