This is a total newb question. Loving powershell, but 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
then I Execute 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. Any idea what I'm doing wrong?