Parameter binding confusion in a pipeline

Hello everyone,

I am struggling to understand the way the parameters for cmdlet in the pipleline are bound. To elaborate, I came across the below article from Boe Proxs website

 http://learn-powershell.net/2015/03/31/introducing-poshrsjob-as-an-alternative-to-powershell-jobs/

In that PoshRSjob module, I am looking at the Start-RsJob cmdlet in particular.

He has shown the below example where I am having trouble understanding:

$Test = 42

1..5|Start-RSJob -Name {"TEST_$($_)"} -ScriptBlock {

    Param($Object)

    $DebugPreference = 'Continue'

    $PSBoundParameters.GetEnumerator()
| ForEach {

        Write-Debug $_

    }

    Write-Verbose "Creating object" -Verbose

    New-Object PSObject -Property @{

        Object=$Object

        Test=$Using:Test

    }

}

My understanding is that the following parameterset is being employed in the above Start-RSJob command:

Start-RSJob [[-ScriptBlock] <scriptblock>] [-InputObject <Object>] [-Name <Object>] [-ArgumentList <Object>] [-Throttle <int>] [-ModulesToImport <string[]>] [-PSSnapinsToImport <string[]>] [-FunctionsToLoad   <string[]>]  [<CommonParameters>]

In this case what I am struggling to understand is

  1.        How is the pipeline object (1..5) bound to the Param($object) in the scriptblock? What is the parameter binding rule being employed here so as for the pipeline object to end up inside the Scriptblock?
  2.        Secondly, what is the significance of [-inputObject <object>] parameter in the above parameterset which however is not explicitly used in the Start-RSJob command above? Does it mean that as long as the object comes from the pipeline we do not need to use this inputobject parameter explicitly, instead it is inferred?

May be a PowerShell parameter binding-101 question but I wanted to fully understand.



July 11th, 2015 6:19am

YOu are asking basic "how to" and "what is PowerShell" questions.

Start here for a detailed explanation of how parameters and parameter sets are used.

Help for these:

about_CommonParameters
about_Functions_Advanced_Param
about_Parameters
about_Parameters_Default_Values

You can also get basic PS info here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

Questions about the specific function should be posted to the author.

As for:

#1 - unnamed parameters are bound by position.  (see help)
#2 - Please read the referenced blog for the explanation of this parameter.

Free Windows Admin Tool Kit Click here and download it now
July 11th, 2015 7:41am

Thanks, I have gone through the links. But I still do not understand how the pipeline object (1..5) bound to the Param($object) in the scriptblock. 
July 11th, 2015 2:58pm

It is in the help.  The explanation is to long and complicated for me to give you here.  It has to do with how parameters are defined.

Try some things.  Give your brain a little exercise.  It will help you to understand things easier.

Try this:

1..5 |%{$_}



function test{
    Param(
        [Parameter(ValueFromPipeline)]
        [int]$a,
        [int]$b
    )

    process{
       "a-->$a"
       "b-->$b"
    }
}
1..5 | test
1..5 | test -b 999

Free Windows Admin Tool Kit Click here and download it now
July 11th, 2015 3:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics