I need to create a powershell script that reads in two text files with lists.
The first list is used to identify the remote computers that will have a file appended to it.
The second list is the key phrase that needs to be appended to the file.
List 1:
Computer1
Computer2
Computer3
List 2:
ABC
DEF
GHI
Script would loop through each pointing to a file say C:\temp\help.txt on each of the remote computers and write 1 line from List 2.
So Example: When the script runs it goes out to List 1, finds that computer 1 is first and opens the file \computer1\C$\temp\help.txt, it would then grab the first line from List 2 and write to the file ABC. Close the file and go on to Computer 2. Computer 2 would be \computer2\C$\temp\help.txt and would grab the 2nd item from List 2 and write to it DEF save and move on.
It’s been hard to find any help reading in and looping through 2 lists. Or perhaps I am thinking of it wrong. I have gotten to Get-Content to read in the file and foreach($x for text1) can go through 1 of the text files loops but can not figure out how to loop through the 2nd text file.
Processing side-by-side arrays is always a pain, and tends to be error prone. Ideally, as suggested, the computer names and strings would be together in a CSV or something.
But as is, something like this should get you on the right track. You might have to fiddle with newlines at the beginning/end of the strings a bit.
If you have it in a CSV like
Then you could simplify to this: