Solved

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

Posted on 2004-09-29
11
209 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now