Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting for newbies: Batch Scripts

General Troubleshooting

Scripting for newbies: Batch Scripts

by  Stevehewitt  Posted    (Edited  )
Hello!

I recently helped a TT member with batch scripting. He had to previous experience of this and was new to IT - so after writting so much back in that post I thought that a FAQ on it may help other people in the same situation.

There are two major types of scripting used in a Windows networking enviroment. These are Windows Scripting Host (WSH) and Batch Scripts. WSH is a relativity new scripting technology that came out in Windows NT. Its a lot more powerful than Batch Scripts but is also a lot more complex. Also there are different version depending on the OS. A WSH script I use for installing printers and defaulting them for my clients (XP) will not work on my Windows 2000 Pro workstations!
Batch Scripts are very easy. They are DOS based, therefore very easy to learn.

Every Administrator needs to know scripting. As powerful as Group Policies maybe in a Win2k enviroment, its not always enough.

OK, Well the only way that I learnt to do batch scripting is by learning DOS. Batch scripts are very, very basic scripts. For example I have a script that sends a message to three workstations telling them that a server will be restarting shortly, I would use the net send command. For example, if I wanted to do it manually I would go to Start - Run - "CMD". In the Command Window I would type in:

Code:
net send [name or IP of workstation] [message]
(Then hit enter!)
Code:
net send [name or IP of workstation] [message]
(Then hit enter!)
Code:
net send [name or IP of workstation] [message]
(Then hit enter!)

Now if I wanted to do this automatically I would open up notepad and type in:

Code:
rem Informing users of server shutdown
@ Echo Off
net send [name or IP of workstation] [message]
net send [name or IP of workstation] [message]
net send [name or IP of workstation] [message] 

rem End script

I would then go to Save As, change the file type to 'All Files' and save as 'shutdownmessage.bat'.

From now on, I just have to double click this file and it will do all the work for me!

Tools such as the "Net" command are probably used the most in Batch scripts. "Net" will provide you with power to stop/start services, send messages, map/delete network drive and install printers. Go to the Windows Command Prompt (Run - CMD) and type in net /?. This will show you how to use the net command.

Another useful script I use is XCOPY. I have a proxy server that provides its logs in a .log file format. Well to view the logs properly I need to import them into Access. Access doesn't import .log files but will .txt. So I use the following script to change them over!: (Note: The script is cut down. Otherwise it would have 90+ users!)
Code:
rem Script to copy .log file to same DIR but with .txt extension.
@ Echo Off
X:
cd\AllegroSurf\Data\Auditx
copy andis.log "andis.txt" /s /y
xcopy richard.log "richard.txt" /s /y
xcopy tony.log "tony.txt" /s /y
xcopy julian.log "julian.txt" /s /y
rem Script Ends
The "Rem" take tells Windows to ignore this line so that you can insert comments. "@ Echo Off" is a command to stop commands echoing. "X:" changes the drive to where the proxy is installed. "cd\allegrosurf\data\audit" changes the directory to where the files are kept and the "xcopy nameofuser.log "nameofuser.txt" /s /y" is the command to copy nameofuser.log to "nameofuser.txt". The "/s /y" are parameter switches. From memory they stop comfirmation I think. I then set the script to run using the Windows Scheduler every 5 mins. If you want to get a bit more techy type in "AT /?" at the Command Prompt and its a text based version of the Windows scheduler. May want to play with that.

The actual question asked was regarding logging users off at a certian time. Here was the actual response:

As your server is Windows 2000 you will need to install the SHUTDOWN.EXE application. Simply copy the application which is included in Windows XP and floating about on the Internet to your WINNT\SYSTEM32 directory.
The below script will shutdown a remote workstation called SALES1 with a 60 second warning. There is a cusom message ("You logon session has expired..."):
Code:
rem Script to reboot sales1 with 60 second warning
@ Echo Off
shutdown -r -m \\sales1 -t 60 -c "Your logon session has expired. This workstation is shutting down." -f
rem Script Ends
Parameters:

-r Tells Windows to do a restart
-m Name of the remote machine
-t How long the countdown is
-c Custom messatge
-f Forceful. System will close regardless of open applications

If you don't type in the -m and the name of the sales machine then the system will do it to the localhost. If you don't input a countdown then the system will start from 30 seconds, and if no custom message then the message will a default. Now save this file as a .bat and create a schedule for it. Either use the AT command(Go to CMD and type in
Code:
AT /?
) or Windows Scheduler. At the desired time the server will force all machines you have listed in the script to shutdown using the parameters of your script.

A handy script to have on your desktop is a cancel. The parameters are "shutdown -m \\computername -a" That will abort the reboot. Again, no computer name means it will work on the localhost only.

Good Luck!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top