PowerShell Gotcha? Parameterized values and math.

Welcome Forums General PowerShell Q&A PowerShell Gotcha? Parameterized values and math.

This topic contains 3 replies, has 3 voices, and was last updated by

 
Keymaster
4 years, 2 months ago.

  • Author
    Posts
  • #19089

    Keymaster
    Points: 17
    Team Member
    Rank: Member

    Hey there guys, I just wrote up a thing that I was doing for an AD User creation script and thought I'd share with the class. I'm still in my first year of PowerShell, so I was pretty proud of what I found and how I fixed it. 🙂

    https://www.linkedin.com/pulse/article/20140925161509-21899939-powershell-maths-or-why-is-my-script-creating-2-million-ad-accounts

    If this is the wrong place to put this, my apologies. I didn't see on it in the PowerShell Gotchas eBook, so I thought I'd mention it.

  • #19090

    Member
    Points: 0
    Rank: Member

    Winding up doing string concatenation when you meant to do math (via the + or * operators) is definitely a common gotcha. As you pointed out, assigning types to your parameters is almost always the right thing to do. (And if you don't, expect to have to write some validation or conversion code before you actually do anything with what was passed in by the caller!)

  • #19091

    Keymaster
    Points: 1,674
    Helping HandTeam Member
    Rank: Community Hero

    This is a fine location :).

    As a best practice, you should always type your parameter variables, to help avoid that specific situation with the + operator. It's actually a bit more complex than what you ran into, but really, really vexing.

    Thanks for sharing! If you'd like to write it up for the Gotchas book, we'd be happy to add it.

  • #19092

    Keymaster
    Points: 17
    Team Member
    Rank: Member

    Absolutely! I'd love to write it up. Where should I send it?

    ***EDIT*** Actually, I just saw the attachment button. I've updated the content based on our discussion. Let me know if this is acceptable.

The topic ‘PowerShell Gotcha? Parameterized values and math.’ is closed to new replies.