Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

In Powershell Why might a foreach-object loop not let you retrieve individual values from an array?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...