Link to home
Create AccountLog in
Avatar of cwstad2
cwstad2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PowerShell script to copy file from a number of remote PC's

Afternoon all, I'm looking to retrieve the following file from ~ 100 PC's  \\PC\c$\Program Files (x86)\TeamViewer\Connections_incoming.txt and was wondering what the best method would be. Ideally i'd like to append a single file and include the name of the host that it was retrieved from. Failing that a separate file for each host.

Many thanks
Avatar of DEMAN-BARCELO (MVP) Thierry
DEMAN-BARCELO (MVP) Thierry
Flag of France image

Technically, if computers belong to the same domain, and if computers are started and accessible, it is not very complicated using a domain admin account.
Now, in your script, you have to manage stopped/inaccessible computers and that is a little more complicated.

The list of 100 computers can come from a single txt file, or from a request in the domain.
What is your situation?
Avatar of cwstad2

ASKER

Good evening, yes single domain and have domain admin. I can put the computers in a txt file and it can the be called as a var.

Many thanks
Here is a sample of script
The first line must be selected according to AD or Text file choice (One name by file)
#$x=get-adcomputer -filter * -SearchBase "OU=LAPTOPS,DC=DOMAIN,DC=COM"
$x=get-content .\computers.txt


# create/clear a file
Echo "" >allContentFiles.txt


foreach ($i in $x){
  $n=$i
#  $n=$i.name (if computername comes from AD)
  ECHO $n >>allContentFiles.txt
  TYPE "\\$n\c$\Program Files (x86)\TeamViewer\Connections_incoming.txt"  >>AllContentFiles.txt
}

Open in new window

Avatar of cwstad2

ASKER

Hi im getting the following error

Echo.>allContentFiles.txt : The term 'Echo.>allContentFiles.txt' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
I believe the . is a typo in Echo.>allContentFiles.txt 
Avatar of cwstad2

ASKER

Hi Andrew any pointers please.
No, the dot is not a typo!

If you don't put a point, you have a space in the file.

With the point, the file has a size of 0.

The line is now adapted to Powershell in the script !
Avatar of cwstad2

ASKER

Hi Thierry, do you have any other suggestions. When I run a get-content on a single machine on it works.

Get-Content -path "\\PC\c$\Program Files (x86)\TeamViewer\Connections_incoming.txt"
Avatar of cwstad2

ASKER

I was thinking something like this, which works but not for multiple hosts within the file. I've also tried with | export-csv c:\users\user\documents\AllContentFiles.txt and still doesn't work.

 $computers = Get-Content -Path C:\users\user\documents\PCs.txt
 foreach ($computer in $computers)
 {
     Get-Content -path "\\$computers\c$\Program Files (x86)\TeamViewer\Connections_incoming.txt"
 }
You made a little mistake inside the loop.

Remove a "s" to the computer variable 
Avatar of cwstad2

ASKER

Hi Thierry it makes no difference. I've tried multiple iterations and still the same
What is happening with your script?
Can you try to use the command Type in place of get-content?
How have you entered names in the text file? One by line, without any space?
I believe the . is a typo in Echo.>allContentFiles.txt
Sorry, it was a remainder of a classical batch (CMD).

That can be replaced by :

Echo "" >allContentFiles.txt 
Avatar of cwstad2

ASKER

Hi Thierry, didn't work unfortunately 
ASKER CERTIFIED SOLUTION
Avatar of DEMAN-BARCELO (MVP) Thierry
DEMAN-BARCELO (MVP) Thierry
Flag of France image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer