Wednesday, November 5, 2008

Free Deployment System Scripts

All Network Administrators at least once thought about how to install a program on remote computer without ever touching it. Hopefully, today we have a lot of ways to achieve this goal. The very interesting part of it all is, when ever I provide a consulting services to companies, I hear the same question, “Michael, do you know any FREE solutions to distribute programs, patches or run anything on remote computers?

I’d like to show you the solution, I’ve created for myself. Let’s call it “Free Deployment System :)”...

Free Deployment System” contains a set of scripts and free tools. The heart of this system is a PSExec by Sysinternals.

Before I start, let’s clarify few pre-requirements. We need one server or computer that will hold off packages and scripts. The Deployment System will run on this system. We need a user with administration privileges on all remote computers (We can create a user on Domain level and set the necessary permissions to this user).

Ok, now we are ready to start.

Let’s created a directory structure on deployment server.

C:\Operation

|_Scripts
|_Tools
|_Packages
In my situation, I don’t have any files in C:\Operation directory. In C:\Operation\Tools directory I have tools that I have been using in my scripts. The main scripts of “Free Deployment System” are located in C:\Operation\Scripts. C:\Operation\Packages contains packages for remote installation. Set a Share on the C:\Operation\Packages and give all users Read Permissions.

Now we need to create a few files in C:\Operation\Scripts directory. The first file is #Servers.txt. This file contains an IP addresses or Computer Name of remote computers. Each IP or name should be on new line. In my case #Servers.txt looks as follow:

192.168.3.95

192.168.3.96
192.168.3.97
192.168.3.98
192.168.3.99
192.168.3.10

The next file in our system will be a Config.bat. By using Config.bat I set credentials for connection to remote computers. Config.bat looks as follow:

@echo off

set DM=AdminInfo
set USR=TSAADMIN
set PASS=TSAPassWord

set DSRV=192.168.211.1

OK, now we are going to create a main script of our system. I named it INSPKG.bat, meaning “Install Packages”. Before show the content of INSPKG.bat I’d like to say a few words about how it works.

INSPKG.bat runs in loop and checks each line of #Servers.txt file and use this addresses as a destination where the installation package should be installed. When the package installed on last computer (last line in #Servers.txt file) INSPKG.bat will stop working. If you have too many computers in your network, the package deployment could take time, be patient. So, INSPKG.bat contains the following lines:

@echo off


Rem ****************************************************************
Rem * For 1 envirement use the Pakage file name *
Rem * The general location of all Pakage is: C:\Operation\Packages *
Rem * On remote computer computer should be located "C:\Updates" *
Rem * *
Rem * http://thesystemadministrator.com - http://www.admininfo.ca *
Rem * *
Rem ****************************************************************

if {%1}=={} @Echo Please set the Pakagename.The format is INSPKG.bat 7Zip &goto :EOF

set Scripts=C:\Operation\Scripts
set Tools=C:\Operation\Tools
set Packages=C:\Operation\Packages

call %Scripts%\Config.bat

SET IP-SRV=
SET LoopNum=
SET Line=
SET I=
SET RCOMP=
SET Line=0

TYPE %Scripts%\#Servers.txt > %TEMP%\SRVLIST.txt

FOR /F "TOKENS=*" %%a in ('%Tools%\LINEX -c ^<%TEMP%\SRVLIST.txt') do set IP-SRV=%%a SET /a LoopNum=%IP-SRV% + 1 :LOOP1 SET /a I=%I% + 1 SET /a Line=%Line% + 1 IF "%I%"=="%LoopNum%" goto END FOR /F "TOKENS=*" %%a in ('%Tools%\LINEX -l %Line% ^<%TEMP%\SRVLIST.txt') do set RCOMP=%%a net use \\%RCOMP%\ipc$ /user:%USR% %PASS% %Tools%\psexec.exe \\%RCOMP% -u %USR% -p %PASS% -i cmd /c md C:\Updates @echo set DSRV=%DSRV%> \\%RCOMP%\C$\updates\config.bat
copy "%Packages%\%1\%1.bat" "\\%RCOMP%\c$\Updates\" /Y

@echo Please wait ...

%Tools%\psexec.exe \\%RCOMP% -u %USR% -p %PASS% -i C:\Updates\%1.bat

SET Line=%Line%
goto LOOP1

:END
SET IP-SRV=
SET LoopNum=
SET Line=
SET I=
SET RCOMP=
SET Line=

INSPKG.bat should run from a console (cmd.exe) of our deployment server. The format is following:

C:\Operation\Scripts\INSPKG.bat 7Zip

Where, 7Zip is a name of a package. As you can see, in this example, I’m going to deploy a 7-Zip to all computers, but before this I have to create a package. So, for this purpose I’m going to the C:\Operation\Packages directory and create a new directory, 7ZIP. Inside 7ZIP I create a file 7ZIP.bat and put the installation file of 7-Zip. I’m going to install a 7z442.exe on all computers. By using RTFM rule :), I know that to install a 7-Zip in silent mode I have to use switch /S. So, my 7Zip.bat contains the following lines:

@echo off

call c:\updates\config.bat
\\%DSRV%\packages\7ZIP\7z442.exe /S


And, on the final, we need put necessary tools to the C:\Operation\Tools directory. Download last version of PSExec and put it to C:\Operation\Tools directory. In INSPKG.bat I’m using a very cool tool, LINEX.EXE. Linex.exe is a part of “Bill Stewart's freeware Batch Script Tools”. When I wrote this article, I tried to find an official web site of “Bill Stewart's freeware Batch Script Tools”, but failed. So, you can download a Linex.exe directly from my web site.

As additional example, I’ve created packages for Acrobat Reader and Firefox. Keep in mind to create a separate directory for each package. The Directory and name of package script should be the same, meaning if you are going to create a package for Firefox inside C:\Operation\Packages create directory Firefox and inside C:\Operation\Packages\Firefox create a script Firefox.bat and also inside C:\Operation\Packages\Firefox put the installation file of Firefox. Here is my Firefox.bat:

@echo off

call c:\updates\config.bat
\\%DSRV%\packages\Firefox\FirefoxSetup2.0.0.9.exe -ms
I hope I didn’t forgot anything :) .
Let me know what kind of package you are interesting in and I’ll try to create it. I’ll be very happy if you write comment for this article.

No comments: