0

Note the following output. $Names is an array with two values. $Machinename is an array with two values.

Their positional values within the array are retrieved accurately outside of a foreach loop. When fetched within a foreach loop the value requested for the first position in the array i.e. $Names[0] ignores the positional call of [0]..... I need to be able to retrieve that value by itself..... Ultimately I will need to interate through each value to input to a command...

PS C:\Users\lab> $Names

john
jeff
PS C:\Users\lab> $Names[0]

john
PS C:\Users\lab> $Names[1]

jeff
PS C:\Users\lab> $Machinename

dev1
dev2
PS C:\Users\htlab> $Machinename | ForEach-Object  { Write-Output "$Names[0]"}

john jeff[0]
john jeff[0]

Sample Script:

$Names = 'john', 'jeff'
$machinename = 'dev1', 'dev2'
$Machinename | ForEach-Object  {Write-Output "$Names[0]"}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Jryan
  • 1
  • 2
    This is due to the `"` double-quotes - the string expansion routine in PowerShell doesn't recognize complex expressions. `$Machinename | ForEach-Object {Write-Output $Names[0]}` will work just fine – Mathias R. Jessen Jan 28 '21 at 00:31
  • [This answer](https://stackoverflow.com/a/50215337/45375) to the linked duplicate provides a detailed explanation, but in short: inside `"..."`, if you want to reference an expression that goes beyond a simple variable reference (e.g., `$var`), you need to enclose it in `$(...)` (e.g., `$($var[0])`) – mklement0 Jan 28 '21 at 02:55
  • Thank you. This answered the immediate question and helps me understand how to reach my later objective. Gave me the fish, the pole, and the know how! – Jryan Jan 28 '21 at 03:48

2 Answers2

4

You're not evaluating the array. By writing "$Names[0]" it is the equivalent of $Names + "[0]".

You need to nest the evaluation inside $(...).

Try this:

$Machinename | ForEach-Object  {Write-Output "$($Names[0])"}

That gives me:

john
john

Equally, as pointed out in the comments, this works too:

$Machinename | ForEach-Object  {Write-Output $Names[0]}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thank you for explaining what was actually happening. Knowing that actually gave me an idea for when i could use something like this when i actually want this behavior – Jryan Jan 28 '21 at 03:49
0

You could try something like this, as it seemed to work for me, unless thats not what you're asking,

for (($i = 0); $i -lt $names.Count; $i++)
{
 $Names[$i] + ":" + $Machinename[$i]
}

Output:

John:dev1
Jeff:dev2
Brnsn
  • 33
  • 6
  • 2
    This doesn't answer the question. It might give the OP the desired result, but the specific question is "Why might a foreach-object loop not let you retrieve individual values from an array?" – Enigmativity Jan 28 '21 at 00:58
  • @Enigmativity fair enough! – Brnsn Jan 28 '21 at 01:05
  • Thank you for the incrementor syntax, while it doesnt address my immediate question, as novice as i am to this, its sure to be helpful with one of my other automation tasks. – Jryan Jan 28 '21 at 03:53