Solved

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

Posted on 2004-09-29
11
211 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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