For all of you who are still out there working because you missed becoming a DOGEionaire and therefore still have your job in IT, this post is for you. And as it’s clearly evident by me writing this post…I did not catch that sweet meme train to the moon. Before we all start grumbling about missed opportunities, let’s take a look at some code. At work recently I was writing a simple function for something I wanted to turn into a one-liner in the future and I ran into a conundrum: that cursed ValueFromPipeline parameter settings in PowerShell. Don’t get me wrong, I actually like that blasted thing but today it was besting me like the hedgies have been doing for months now.

\\\What had happened was

First, let’s talk about what happened. There I was, minding my own business, shelling out power quicker than you can mine a bitcoin and I ran into an error. Let me just segue right here into my theory that if the error messages were colored green instead of red there would be a lot less anxiety in the IT world…but that’s a discussion for another day. If you’re unfamiliar with the ValueFromPipeline parameter attribute, in a nutshell it allows you to pass a value from the pipeline to this parameter in your function. It’s really useful for getting something with one cmdlet and then piping it to the next cmdlet to do something with it. I’ve used this hundreds of times before and this was the first time it wasn’t working for me.

So I did what any good IT professional would do: I kept pressing enter thinking it would fix itself.

After about half a day of doing that and getting nowhere, I decided to actually look at my code. This wasn’t my actual code (or was it?), but here’s an example of what I ran into:

function FixIt{
    [CmdletBinding()]
    Param(
        $FixerUpper,
        [Parameter(ValueFromPipeline=$true)]
        $ThingThatNeedsFixin
    )
    Begin{
        $CertifiedName = (Get-ComputerInfo).$ThingThatNeedsFixin
    }
    Process{
        Write-Host "$FixerUpper is going to fix the $CertifiedName!"
    }
    End{}
}

\\\Stonks Only go UP

Since my parameter $ThingThatNeedsFixin accepts value from the pipeline, I expected to be able to do something like this:

'OsManufacturer' | FixIt -FixerUpper  'Felix'
FixIt Function
But lo and behold an error awaited me on the other side of the terminal. At least I think it was an error. Instantly I had worries of inflation and share dilution and contemplated selling my scripts at a fraction of what I paid for them, but I pushed those thoughts out of my head and went back to the drawing board.

What’s worse than an error message written in red ink? No error message at all. I checked all of my notes, googled ValueFromPipeline to make sure I had typed it correctly, and even buried myself in multiple volumes from the Library of Alexandria to no avail. What was I missing? Everything looked right…but it still wasn’t working.

\\\Bull Trap

Finally, I found my issue.

Well, one of my issues. I still have plenty of other ones.

If we move this line from here:

FixIt Function 1
To Here:
FixIt Function 2
And try, try, and try again:
FixIt Function (Working)

Well I Musk say, it worked! I went back to the BEGINning and found the culprit! I was today years old when I learned that the ValueFromPipeline only works in the Process block of a function…not the Begin block.

\\\ID10T

And for all of you out there shaking your head at me…yes, I know it makes perfect sense when you stop and think about it. The Begin block of a function runs once and the Process block exists to loop through each value piped to it when you set a parameter to allow values from the pipeline. Duh! I mean, of course it wouldn’t work in the Begin block…I had just never made that mistake before so I was lost for longer than I care to admit. I think it was the DOGE FOMO getting to me.

Maybe you already know this and you learned absolutely nothing from this post. Or maybe this will help you in the future and save you some time writing advanced functions in PowerShell. In either case, let’s go the MOOOOONNNNNNN BABBYYYYYY!

OTHER POSTS YOU WANT TO READ

Index Scripts for Windows Search

So you just finished writing some code and you go to save your file. You summarize all of the important aspects this section of code contains into a nice, easy-to-read file name that your future self will immediately recognize. Fast forward to the future where you...

Array vs ArrayList (PowerShell)

For some tasks in life, being precise is a necessity. But most of us get away with rounding, paraphrasing, and hitting in the general vicinity most of the time. Depending on your personality, you may be one who strives for perfection and strains on every miniscule...

Spice Up HTML Emails with PowerShell – Part III

So far in this series we've fumbled our way around the kitchen and tried to wing it when sending HTML emails with PowerShell. It was clunky to say the least. We then went through our spice rack and built an HTML template, highlighting the nuances of each spicy element...

Spice up HTML Emails with PowerShell – Part II

In Part I of our scrumptious concoction we put our script into the oven to let it bake. But we forgot to add our secret sauce that's sure to leave our recipients drooling, which is clearly our goal. In this post we'll continue to spice up HTML emails with PowerShell...

Spice Up HTML Emails with PowerShell – Part I

I live in the South, specifically in God's country AKA TEXAS BABAY! There's plenty of amazing things about Texas and definitely some reasons to loathe being from Houston, but it's hard to knock our food. I mean, is there really even a debate between Whataburger vs...
%d bloggers like this: