ScriptRunner Blog
Parameter Binding concepts in PowerShell
Table of Contents
Parameter binding is a fundamental concept in PowerShell. And you can end up scratching your head for a while if you do not understand this concept fully, as I recently experienced first-hand.
In my case it was a simple re-read of advanced functions and then trying a simple script to make a calculator, in order to re-test how much I can recall. The problem started when I tried to supply parameter values from the pipeline. In this blog post I will be going over two fundamental PowerShell pipeline parameter binding concepts: “byValue” and “byProperty”.
Let us examine my first attempt at my calculator program. As you can see below it is a simple PowerShell script to add, subtract, multiply and divide two numbers (Figure 1a + 1b).
Here is where the problem starts: when I try to create a custom object with properties same as my function parameters and pass it to Get-Calculation function in the pipeline, it gives me an error (Figure 3).
Troubleshooting with Trace-Command
We can observe in figure 5 that instead of binding object properties to parameter the “whole” object ($myObj) is supplied as value to the parameter.
Parameter Binding: byValue vs. byProperty
“Bind by Value” means that the incoming object will be supplied as value to the parameter. The Pipeline will not process the object and bind specific properties of the object to parameters for the next cmdlet/function in the pipeline. If we observe the output from Trace-Command, the pipeline is trying to bind our object ({firstNumber=1; secondNumber=4; operator=+}) to the parameter “firstNumber” and so on.
The solution is to bind “byProperty”. In our example we need to explicitly tell the pipeline to bind the incoming object’s properties to our parameter. As shown below we changed our original script to bind byProperty (Figure 6).
Next, we again observe the behavior using Trace-Command. As we can see this time, object properties are bound to our parameters correctly (Figure 7). However, there is one catch: in order for this to work the incoming object’s properties should have the same names as the function parameters.
Test: Parameter Binding byValue and byProperty
Now our function accepts parameter values using both the byValue and byProperty option. Let’s take a simple example where we send two object one of type Int (Figure 9) and the other a custom PS object (Figure 10).
The first result where we got 8 might be a bit confusing but the program worked as expected for second object (In case you’re wondering out operator property, I will leave it as an homework for you).
We are going to observe binding using our good old friend the Trace-command. As you can observe in Figure 10 the first object, number 4 of type Int is bind to firstNumber and secondNumber resulting in sum being 8. This demonstrates that the pipeline will plug in the same object to all the parameters which are accepting value from the pipeline if we are using “byValue” option.
In the case of our PS custom object, we can observe that the pipeline first tries to bind byValue but it fails and then, as expected, switches to byProperty option and starts binding parameter by property name (Figure 11).
Conclusion
Here are some key takeaways from this post:
- Default option for parameter binding is to bind by value.
- If we have selected both byValue and byProperty option, the pipeline will start by binding using byValue option.
- If the parameters are supplied directly to the function, then the process block is executed exactly once.
- If the parameters are supplied by pipeline, the process block is executed once for each object in the pipeline.
I hope this post would help you to further understand and implement advanced functions correctly.
Related Content
Related posts
6 min read
Boost your IT automation efficiency with new ScriptRunner release
Sep 30, 2024 by Frank Kresse
We have just released our latest ScriptRunner update, version 7.1, packed with powerful new features aimed at making IT...
8 min read
Scriptember 2024 – Celebration of PowerShell and its community
Aug 16, 2024 by Heiko Brenn
Welcome to Scriptember! We are thrilled to announce the launch of a unique, month-long campaign dedicated to...
10 min read
Five reasons you should be using PSReadLine
Aug 14, 2024 by Jeffery Hicks
I'd like to think that because you are reading this, you are a professional PowerShell user and this article will be a...
About the author:
Sonny is a self-proclaimed PowerShell preacher who lives in the beautiful city of Halifax on the east coast of Canada. Sonny has worked in Cybersecurity for more than 10 years and has acted as the primary technical lead and subject matter expert on many Cyber Security Assessments for various private and public organizations. Sonny regularly speaks at various security conferences such as BSides, AtlSecCon, ISACA, OWASP etc.
Latest posts:
- Boost your IT automation efficiency with new ScriptRunner release
- Scriptember 2024 – Celebration of PowerShell and its community
- Five reasons you should be using PSReadLine
- Privacy Management with PowerShell – Let's look at the core features of Priva!
- Let's celebrate System Administrator Appreciation Day!