Parameters and powershell functions?

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

The problem is that you need to convert your code to the following Repeat-string "x" 7 In PowerShell anytime 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.

Thanks! I knew I was doing something stupid -- just wasn't sure what. – JMarsch Jun 5 '09 at 19:33 @JMarsch, everyone gets tripped up by this at least once.It's really annoying :( – JaredPar Jun 5 '09 at 19:33 I hate this!

But you get used to it... – Philippe Jun 8 '09 at 13:13.

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.

Every new thing I learn about powershell just shows me how cool it is. Thanks for sharing that, Shay +1 – JMarsch Jun 7 '09 at 17:30 PowerShell ROCKS ;-) – Shay Levy Jun 8 '09 at 17:14.

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: (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...).

Of course JaredPar is right. I like to use the built in range function .. for this too: (notice I start at 1 instead of 0) Function Repeat-String(string$str, int$repeat) { $builder = new-object System.Text. StringBuilder 1..$repeat | %{ void$builder.

Append($str) } return $builder.ToString() } Repeat-string "x" 7.

Thanks -- didn't know about the range function. I'm going to have to fit a book on powershell into my reading list. (I guess at this point, I might as well look/wait for a powershell 2.0 book) – JMarsch Jun 5 '09 at 19:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions