Running a command on different computers

by flay117 at 2013-02-19 03:10:11

Hi all,

I’m pretty new to powershell, and I’m wondering if anyone could explain how I could run a command on different computers, with the computernames in a text file.

What I need to do is to browse to each computers C:\ and have a look in a text file on each computer, and then paste that info in a text file on my local C: drive. So the command would look something like this: (the computers.txt consist of \computername\c$)

$computer = (get-content computers.txt)
cd $computer
get-content test.txt | out-file -append C:\results.txt

How could I modify this script to be able to do this on each one of these computers, if I put more names into the computers.txt?
by jonhtyler at 2013-02-19 04:33:12
Since you are using the UNC convention to address your computers, I am going to assume that this is how you have been accessing the files and that all firewall rules are in place to get to the remote computer and its administrative shares. That being said, you could use something like the following:

foreach ($path in (get-content -path computers.txt))
{
get-content -path "$path\test.txt" | out-file -path c:\results.txt -append
}