Wide-Spread File Deployment With VBScript
Quite often at work we need to deploy one or more files to large numbers of computers. The long way to do it is to manually copy to each computer through the network one at a time. When an entire school needs to be done, it gets very tedious. Another option is to set up a drag-and-drop event in Altiris Deployment Console, our deployment solution, which can then be deployed to an entire school in one shot. However, it can be annoying to set up a new event for every file that needs to be moved, and there are certain files it just doesn’t like to move (.url files for example).
Either my ‘hacker’ side who wishes to come up with creative solutions to problems, or my lazy side, led me to throw together a quick and dirty vbscript to take care of this. It had to be simple yet flexible enough that no code needs to be edited to change what files are being copied. The most challenging part was that I didn’t know anything about vbscript.
The reason I picked vbscript is because it can be executed on any Windows XP machine, so the work can be done from anywhere on the student VLAN. No compiling needed, and it’s more powerful than a batch file.
The script asks for four pieces of input. The file or files being copied, the directory on the remote computers to put the file in (same place on every machine), an input file containing a list of computers, and a log file to write to, so that we can manually hit any computer it missed.
Taking the input and copying the file are both very basic operations which anybody can learn to do with vbscript after a little googling. At the heart of the script is the LineParser function, which takes input from the computer name list and passes computer names to the CopyFile function. At first the computer name list file was just a list of full computer names, one per line, which were passed directly to the CopyFile function from the file system object. However, our computers are named schoolnumber-roomnumber-computernumber (##-[###|####]-##), so some loops should easily work. I then added the LineParser function to allow the input file to have one of three things on each line: a full computer name, a school number, or a room number and the number of computers in that room. Each line will either be ##-###-## or ## or ### ##. LineParser determines which of these three patterns is present on a line and acts accordingly.
If the line contains a school number (only two characters on the whole line):
If Len(LineReader)=2 then then the SchoolNumber variable is changed to that new school number. Any subsequent lines containing a room and number of computers will use that school number to construct computer names, until another school number line is encountered.
SchoolNumber=LineReader
If the third character is a ‘-’ then the line must be a computer name. It is passed directly to the CopyFile function:
ElseIf Mid(LineReader,3,1)="-" then
FNameS = "\\" & LineReader & "\c$\" & MoveToPath
CopyFile(FNameS)
You can see the UNC path being built up from the rest of the user input.
If these two criteria are not met, then the parser assumes the line contains a room number and number of computers. If not, then the user constructing the input file screwed up, and I didn’t put any error protection in there for that so oh well. A loop then begins, passing computer names to the CopyFile function that are constructed using the room number and the last school number specified:
Else
Room = Split(LineReader," ") RoomNumber = Room(0)
NumComps = Room(1)
for i = 1 to NumComps
if i < 10 Then
iFixed = "0" & i
Else
iFixed = i
End If
CompName = SchoolNumber & "-" & RoomNumber & "-" & iFixed
FNameS = "\" & CompName & "c$\" & MoveToPath
CopyFile(FNameS)
Next
End If
The vbscript function Split splits an input line at the delimiter given (space in this case) and tosses them into an array (so you end up with an array of words). The loop then builds computer names and passes them on. Fun stuff.
All of this results in allowing a flexible pseudo-language to be used to specify computers. an input file may look like this:
82 These 8 lines would result in the file(s) being copied to 131 computers. Note that the school number 82 persists past the two explicitly listed computers, until the school number 80 is specified.
126 32
128 32
LIB 20
25-101-01
35-105-03
133 25
80
LIB 20
The entire script can be found here. I plan to rearrange the if statement so that full computer names are the Else, so that any computer name can be used, not just those that follow the ##-… pattern. There are some other functions I’d like to add, but right now it does everything we need.
I learned two things in the two hours I spent doing it. I learned vbscript, and I learned that I don’t like vbscript. It is very inconsistent. It is certainly more powerful than batch programming, but doesn’t match perl, python, or any of the like for scripting. Oh well, it was fun.
