-3

I try to find out how to merge 2 arrays name, this is what I try to run but it didn't works

for ($i=1; $i -le 4; $i++) {
    $test[$i] = Invoke-WebRequest http://lon-serv-$i/mani.json -TimeoutSec 30 -ErrorVariable RestError -ErrorAction SilentlyContinue
}

What I want to happen is that I'll have 4 arrays:

$test1 
$test2 
$test3 
$test4

Anyone knows what I do wrong?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Hay
  • 1
  • doesn't work how? Do you get an empty array (no response from website maybe?), only one element in array, an error message, or do you just get a single `$test` variable? – LinkBerest Dec 25 '17 at 16:24
  • I get error message with the following error "Cannot index into a null array", then I added $test=@() in the beginning and I get error message again with the following error "Index was outside the bounds of the array" – Hay Dec 25 '17 at 16:26
  • Regarding the response from the website - there is no problem with this, I always get a response. – Hay Dec 25 '17 at 16:32
  • 4
    `$test` is not declared as an array from what I see here. Also PowerShell arrays are of fixed size. If you want dynamic variable names you need to use `New-Variable` but I think you would be better of using arraylists – Matt Dec 25 '17 at 20:26
  • It's not clear to me at all what you want to achieve here. Do you want to have 1 array variable with 4 elements in that array? 4 variables? 4 arrays? What do you want to merge? And how? Examples usually help illustrate what you have and what you actually want to accomplish. – Ansgar Wiechers Dec 27 '17 at 15:07

1 Answers1

1

you are telling powershell to get the object inside $test[$i], you not declaring a new variable called $test1/$tets2 . you can use an array of web requests like this:

$test +=@()
for ($i=1; $i -le 4; $i++)
{
    $test += Invoke-WebRequest http://lon-serv-$i/mani.json -TimeoutSec 30 -ErrorVariable RestError -ErrorAction SilentlyContinue
}
M.Pinto
  • 46
  • 2