Parameter binding between scripts

Welcome Forums General PowerShell Q&A Parameter binding between scripts

Viewing 2 reply threads
  • Author
    Posts
    • #193556
      Participant
      Topics: 3
      Replies: 5
      Points: 119
      Rank: Participant
      I have two functions that I have included in a module for personal use. I decided to share them as scripts since I know it is more likely that someone will run a script than install a module.
      In summary, the first one gets a group of VMs from a VMware vCenter and the second one will look for these VMs' IP addresses and open a RDP session to each one of them.
      It works as expected when running the functions and when the second one is a function, but if the second one is the script and not the function I only get the last VM processed.
      These are all possible scenarios, with the exact commands I used to run them:
      #Both are functions – Working
      
      Get-SgVm -SGNumber 8 -NoRecursion -Legacy | Connect-VMRDPSession -Datacenter DC1

      **

      #First in pipeline is script the second one is a function – Working
      
      .\Get-SgVm.ps1 -SGNumber 8 -NoRecursion -Legacy | Connect-VMRDPSession -Datacenter DC1

      **

      #Both are scripts – Only the last object (VM) of the pipeline gets processed by the second script (only one RDP session is opened)
      
      .\Get-SgVm.ps1 -SGNumber 8 -NoRecursion -Legacy | .\Connect-VMRDPSession.ps1 -Datacenter DC1
      I ran a Trace-Command with ParameterBinding for the last example. The first script gets all VMs as expected but passes only the last object/VM of the collection to the next script.
      I could share the code of these functions and scripts but I think this is more related to the way in which PowerShell processes objects in the pipeline, it seems to be different depending on whether the second element in the pipeline is a script or a function. Is this an expected PowerShell behavior? I am using PS version 5.

       

    • #193562
      Moderator
      Topics: 3
      Replies: 169
      Points: 889
      Helping Hand
      Rank: Major Contributor

      I don't think ValueFromPipeline and ValueFromPipelineByPropertyName behave differently for scripts and functions. Can you check the function is properly converted to script?

      Thank you.

    • #193568
      Participant
      Topics: 3
      Replies: 5
      Points: 119
      Rank: Participant

      I don't think ValueFromPipeline and ValueFromPipelineByPropertyName behave differently for scripts and functions. Can you check the function is properly converted to script?

      Thank you.

      Thanks Kiran. You are right. It was a missing Process block in the script. I have "fixed" it and it is now working fine.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.