Parameters and PowerShell functions

10,998

Solution 1

The problem is that you need to convert your code to the following

Repeat-string "x" 7

In PowerShell, any time you put a group of values inside ()'s, you are creating an array. This means in your sample you're actually passing an array to the function as a single parameter.

Solution 2

Here's a better way, just multiply your (any) string by N repeats:

PS > function Repeat-String([string]$str, [int]$repeat) {  $str * $repeat }
PS > Repeat-String x 7
xxxxxxx

PS > Repeat-String JMarsch 3
JMarschJMarschJMarsch

Solution 3

A noted Chinese proverb states:

"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

For the cosmopolitan-minded among you, the original is 授人以魚不如授人以漁 and the Pinyin romanization is Shòu rén yǐ yú bùrú shòu rén yǐ yú. (I have been learning to speak a smattering of Mandarin and to write Pinyin so I have to practice! Curiously I found the above Pinyin expression seemed to be an enigma, but that is a story for another time...:-)

Now to my point: JMarsch came upon a common PowerShell pitfall liable to trip up anyone used to "conventional" languages, and @JaredPar provided the correct resolution. I submit, though, that answer is akin to slapping down a fish in front of you!

Just published on Simple-Talk.com, Down the Rabbit Hole: A Study in PowerShell Pipelines, Functions, and Parameters discusses the above pitfall along with many other nuances of the function-calling interface. One section of my article, for example, covers the subtle differences between all of the following calls to function f, most of which will not yield what you expect.

f(1,2,3)
f (1,2,3)
f 1,2,3
f (1 2 3)
f 1 2 3

Look for the PDF download of the handy wallchart reference accompanying the article, too.

Here's a thumbnail: PowerShell Functions Quick Reference

(Oh, and you may want to also check out the list of humorous spin-offs of the above classic quote at Give a man a fish...)

Share:
10,998
JMarsch
Author by

JMarsch

Updated on June 16, 2022

Comments

  • JMarsch
    JMarsch almost 2 years

    I'm definitely not getting something here:

    I'm creating a simple function to replicate a string x times. I am having some weird problem with the parameter -- it doesn't seem to be recognizing the second parameter. When I run the function, it returns an empty string. Further, I think it's lumping the 2 parameters into 1. Here's my code:

    
    Function Repeat-String([string]$str, [int]$repeat) {
      $builder = new-object System.Text.StringBuilder
      for ($i = 0; $i -lt $repeat; $i++) {[void]$builder.Append($str)}
      $builder.ToString()
    }
    

    First I dot-source it to load it:

    . .\RepeatString.ps1

    And then I execute it like this:

    Repeat-string("x", 7)
    I expected a string of 7 x's. I got an empty string.

    I went poking around some more, and I changed the "for" loop. I replaced the "-lt $repeat" part with "-lt 5", so that I would get a fixed number of repeats. When I did that, I got the following output (without the quotes):

    Repeat-String("x", 7)

    "x 7x 7x 7x 7x 7"

    It looks as though it is concatenating the $str and $repeat parameters instead of treating them like 2 separate parameters. What am I doing wrong?