?
Solved

Write a bat file that copies files to all computers in an OU

Posted on 2004-09-29
11
Medium Priority
?
222 Views
Last Modified: 2010-04-14
What I would like to do have a bat file I can run either on a domain controller or a file server within my domain that copies a file to all computers within an OU.

The bat file would be run manually and probably not too often, but I don't want to have to go to each machine to get the file on the computer.

Thanks.
0
Comment
Question by:mrichmon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 10

Expert Comment

by:Longbow
ID: 12183142
I would not run a batch file manually to all the computers because
 you never know wich computer have received the files.
If one computer is off your script can stop.

1) You can create a logon .bat script :
@echo off
if not exist c:\destination\filename.ext copy \\server\share\filename.ext c:\destination\filename.ext

2) Another solution would be :
@echo off
regedis /s \\server\share\file2copy.reg
copy \\server\share c:\destination\filename.bat

In filename.bat you have the code found in 1)

in file2copy.reg you have the registry key RunOnce=c:\destination\filename.bat
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12183645
But I do not want this to happen at logon.

I want to run it manually, and immediatley upon running the bat file.
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12184004
Easy.  I would write a piece of VBScript to do this..  Something like:

Set oFS = CreateObject("Scritping.FileSystemObject")
Set oOU = GetObject ("LDAP://OU=<yourOUname>,DC=<yourDomainName>,DC=<TLD>")
For Each computer in oOU
     MachineName = Right(computer.Name, Len(computer.Name)-3)
     oFS.CopyFile "\\server\share\file", "\\" & MachineName & "\share\file"
Next
WScript.Echo("Done.")


HTH,
exx

PS - obviously, it is assumed that all computers listed in the OU are connected to the network and turned on...
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 35

Author Comment

by:mrichmon
ID: 12184870
When I tried that I got the following error:

Line : 1
Char : 1
Error : ActiveX xomponent can't create object: 'Scripting.FileSystemObject'
Code : 800A01AD
Source : Microsoft VBScript runtime error
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12185179
Oops..  Did you cut and paste my script?  I accidentally misspelled scripting...  LOL
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12185282
I'll try again... :o)

I also found that you can have a file that simply has :

copy \\server\share\filename "\\machinename\share\location\"

for each system, but I like the idea that it can be in a for each loop
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12185334
Well that worked for just grabbing the machine name, but when I actually put in the copy line it died when it couldn't access a machine and didn't tell me which machine - nor did it continue on with the rest.

Just putting the copy lines manually into the bat file at least shows a log of what failed and worked and also tries each one regardless of if the last one failed or not.....
0
 
LVL 18

Accepted Solution

by:
exx1976 earned 2000 total points
ID: 12189357
Ok, this one will echo for you..  And I fixed the spelling so you can just copy and paste it..

On Error Resume Next
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oOU = GetObject ("LDAP://OU=<yourOUname>,DC=<yourDomainName>,DC=<TLD>")
For Each computer in oOU
     MachineName = Right(computer.Name, Len(computer.Name)-3)
     WScript.Echo("Copying to " & MachineName & ".")
     oFS.CopyFile "\\server\share\file", "\\" & MachineName & "\share\file"
Next
WScript.Echo("Done.")

Alternatively, if you really like the dos copy command, you could use this script:

On Error Resume Next
Set ws = CreateObject("Wscript.Shell")
Set oOU = GetObject("LDAP://OU=<ou>,DC=<domain>,DC=<TLD>")
For Each computer in oOU
     MachineName = Right(computer.Name, Len(computer.Name)-3)
     ws.run("copy \\server\share\file \\" & MachineName & "\share\file >> c:\copylog.txt")
Next
WScript.Echo("Done.")

To the bext of my knowledge, there is no DOS interface to AD that could be used in a regular old batch file..  At least not without the use of third party tools.  But the second script here should do what you want.  It uses the DOS copy command, and pipes the output to a text log so you can review it.

HTH,
exx
0
 
LVL 10

Expert Comment

by:Longbow
ID: 12189486
You can use a network manager like Landesk to put tasks an any computer on your network :
http://www.landesk.com
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12191620
Thanks a lot!
0
 
LVL 18

Expert Comment

by:exx1976
ID: 12191797
n/p
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Check out the latest tech news, community articles, and expert highlights in August's newsletter.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question