Computing.Net > Forums > Windows 2000 > Script question

Script question

Reply to Message Icon

Original Message
Name: Blorg
Date: October 12, 2004 at 04:21:32 Pacific
Subject: Script question
OS: Win2k Server
CPU/Ram: 1.4ghz/1gb
Comment:

Is it possible to create a simple .bat file that you can input a server name?

I created a .bat file to stop certain services, start the service and then query them to make sure they are running. At this point you have to run it directly from the server so I was curiouse if there is a way to allow the user to input a server for it to run on with out editing the script every time you need to check services on a different server.

Please post any tips and examples of and if you can do this type of thing. Thanks.


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: October 12, 2004 at 04:36:25 Pacific
Reply: (edit)

Just use the Set /P command to prompt the input and store it into a variable, so

Set /P Server=Enter the Server Name^>
SC %Server% Query

The above line prompts "Enter Server Name>" then stores the characters typed into the variable "Server" and uses its content "%Server%" in the SC statement.

I hope this helps.


Report Offensive Follow Up For Removal

Response Number 2
Name: Blorg
Date: October 12, 2004 at 13:23:54 Pacific
Reply: (edit)

Thanks, I will try it out.


Report Offensive Follow Up For Removal

Response Number 3
Name: Blorg
Date: October 12, 2004 at 14:00:51 Pacific
Reply: (edit)

Cool, it works.

Is it possible to specify the server and then specifiy the service to stop? So you could use the file to stop and start any service on any server as long as you have the names to use.


Report Offensive Follow Up For Removal

Response Number 4
Name: IVO
Date: October 13, 2004 at 04:40:17 Pacific
Reply: (edit)

By Set /P command you can make your script interactive as you like, prompting for what you want at any time. So, to follow your question, you may first query for the server name and then for the service you want to stop/start, e.g.

:QUERY
Set /P Server=Enter Server Name^>
Set /P Service=Enter Service^>
Echo.
Echo Server=%Server% Service=%Service%
Echo [S] Start service
Echo [T] Stop service
Echo [Q] Re-enter Server/Service
Echo [X} Exit
Echo
Set Command=??
:REPLY
Set /P Reply=^>
If /I "%Reply%"=="S" Set Command=Start
If /I "%Reply%"=="T" Set Command=Stop
If /I "%Reply%"=="Q" GoTo :QUERY
If /I "%Reply%"=="X" GoTo :EOF
If "%Command%"=="??" GoTo :REPLY

Echo SC %Server% %Command% %Service%
SC %Server% %Command% %Service%

The label :EOF is the standard return point and does not need to be declared.

That is just a sample code, but it should show how to give interactivity to a script. More sophisticated techniques are possible however.


Report Offensive Follow Up For Removal

Response Number 5
Name: Blorg
Date: October 15, 2004 at 03:23:51 Pacific
Reply: (edit)

Thanks a lot for the info.

Do you knof any good sites or books that teach you how to script? Thanks.


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: October 15, 2004 at 06:05:12 Pacific
Reply: (edit)

I can't say how any people asked me the same question at the end of threads similar to this one! And the replay is always the same... I can't suggest neither a book nor a satisfactory web site to learn about scripting.

There are obviously a lot of web sites about batch scripting, but, in my opinion, they all are not good in some way. Try to search by google setting "NT batch scripts" or "files", then help yourself to go on into the jungle.

The reason to set "NT" is due to the hidden (to the most people) difference between the command line in DOS-based systems (Windows 9X/ME) and NT-kernel environments (Windows NT/2K/XP). Despite their commercial logo (Windows) DOS and NT systems belong to absolutely different families; the prompt however is confusing as the NT-system console (the prompt you see in Win 2K/XP/NT) looks like the old command.com DOS prompt.

So you need to learn the basic DOS scripting and then go further into NT scripting that is far more powerfull (e.g. Set /P works under NT-kernel only).

A way to learn about commands is to type the /? switch after their op-code to get the on-line help (e.g. Set /?). That is not a friendly way, but it works if you are a serious student. To follow that way you have however to know the command op-codes and have a basic exposure to scripting structures (If, GoTo, Call and so on).

How did I learn the black magic? Twenty years ago reading the DOS handbook packaged with my first personal computer and then walking through the /? switch while DOS became Windows 9X, NT....XP.

Tomorrow never ends...



Report Offensive Follow Up For Removal

Response Number 7
Name: Blorg
Date: October 21, 2004 at 03:17:34 Pacific
Reply: (edit)

Great, thanks.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Script question

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge