Solved

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

Posted on 2004-09-29
11
217 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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