Powershell function that creates a array by input

Posted by user2971548 on Stack Overflow See other posts from Stack Overflow or by user2971548
Published on 2013-11-09T09:51:05Z Indexed on 2013/11/09 9:53 UTC
Read the original article Hit count: 259

Filed under:
|
|

I'm quite new to Powershell and working on a little project with functions. What I'm trying to do is creating a function that takes 2 arguments. The first argument ($Item1) decides the size of the array, the second argument ($Item2) decides the value of the indexes.

So if I write: $addToArray 10 5 I need the function to create a array with 10 indexes and the value 5 in each of them. The second argument would also have to take "text" as a value.

This is my code so far.

$testArray = @();

$indexSize = 0;

function addToArray($Item1, $Item2)

{

while ($indexSize -ne $Item1)

{

    $indexSize ++;    
}

Write-host "###";

while ($Item2 -ne $indexSize)
{
    $script:testArray += $Item2;
    $Item2 ++;
}

}

Any help is appreciated.

Kind regards Dennis Berntsson

© Stack Overflow or respective owner

Related posts about arrays

Related posts about function