Powershell, loop through two different arrays?

160 pts.
Tags:
Arrays
LOOP statement
PowerShell
PowerShell Scripts
Scripting
Helllo, What I am trying to accomplish is reading in two separate text files and going through them 1 by 1. Say text file 1 is computer names and text file 2 is ABCDEFG (the alphabet) In the script i'm trying to access a remote computer(s) (According to the text file 1) and access a text file on the remote computer. From there i want to insert the letter A in the first computer in text file 1. Then loop through and on the second computer on the list I want to write to it's text file a B. 3rd computer I want to write a C. I was using Get-Object for assigning a value to the arrays for the text files. I have gotten through a foreach to get through each computer, but have trouble trying to loop through the 2nd text file. How do I run two loops where the 2nd text file is going to the next line and writing that output? -Thanks B

Software/Hardware used:
Powershell
ASKED: August 20, 2012  11:12 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

I am not sure I understand exactly but here is what I think you are trying to do. You want to read in two files one with a list of computer names and the other with just the alphabet. You want to read in the comp name from file 1, do some work and then append file 1 with the letter A next to computer one and B next to computer two and so on. Here is what I would do. This script is provided as is with no warranty. If you need anything else contact me at colin@dbyay.com

$file1 = “path to file1 Computer names”
$file2 = “Path to file2 Alphabet”
$comps = Get-Content $file1
$alphs = Get-Content $file2
$cont = 0
foreach ($alph in $alphs)
{
$compname = $comps[$cont]
##do some work on remote $compname
$newname = “$alph” + “$compname”
$newname
$cont = $cont+1
}
You might want to modify this so that it will output to a file and then replace file 1 with the new file. That new file will have the acomp1 bcomp2, etc…

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following