Batch script for maintaing remote services

T

test

Greetings,

I want to maintain (stop/start) services on remote machine in orderly
fashion. The requirement is that the next service should only be
started/stopped when previous service has stopped/started completely.
I am using Windows xp. I am able to come up with something like below:

REM ** To stop services on remote machine **

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

REM ** To start services on remote machine **

start /wait sc \\server_name start service_name1
start /wait sc \\server_name start service_name2


Is it true that above command means that service_name2 will only
stop/
start when the first service i.e service_name1 gets stopped/started
completely?

Also, I have one more issue. Sometimes when the service is getting
stopped, it goes into a "intermediate" state as STOPPING. In this
case, I want to kill this service before sc command go on stopping
next services.

Can someone please help me here. I have searched enough and not able
to come to any conclusion.

TIA
 
P

Pegasus \(MVP\)

*** See below.

test said:
Greetings,

I want to maintain (stop/start) services on remote machine in orderly
fashion. The requirement is that the next service should only be
started/stopped when previous service has stopped/started completely.
I am using Windows xp. I am able to come up with something like below:

REM ** To stop services on remote machine **

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

REM ** To start services on remote machine **

start /wait sc \\server_name start service_name1
start /wait sc \\server_name start service_name2


Is it true that above command means that service_name2 will only
stop/start when the first service i.e service_name1 gets
stopped/started completely?
*** Probably, but if this is what you want then you should
*** drop the "start" command altogether:
*** sc \\Server_Name stop service_name1
Also, I have one more issue. Sometimes when the service is getting
stopped, it goes into a "intermediate" state as STOPPING. In this
case, I want to kill this service before sc command go on stopping
next services.
*** In such cases you need a loop, e.g. like so:
***
*** sc \\Server_Name stop service_name1
*** :Loop1
*** ping localhost -n 10 > nul
*** sc \\Server_Name query service_name1 | find /i "stopped || goto Loop1
***
*** You should also add a loop counter in order to deal
*** with cases where the service can't be stopped.
 
T

test

*** See below.












*** Probably, but if this is what you want then you should
*** drop the "start" command altogether:
*** sc \\Server_Name stop service_name1


*** In such cases you need a loop, e.g. like so:
***
*** sc \\Server_Name stop service_name1
*** :Loop1
*** ping localhost -n 10 > nul
*** sc \\Server_Name query service_name1 | find /i "stopped || goto Loop1
***
*** You should also add a loop counter in order to deal
*** with cases where the service can't be stopped.





- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks. But the requirement says that I have to stop certain services
(stop next service only when previous service has completely stopped)
and then start them in reverse order (again, the next service should
be started only when previous services has started completely).

So based on your inputs I was able to modify the code as below

REM ** stopping first service
sc \\server_name stop service_name1

REM ** looping to make sure service got completely stopped **

:Loop1
ping localhost -n 10 > nul
sc \\server_name query service_name | find /i "stopped" || goto Loop1

Does the loop breaks when it finds "stopped" status for a particular
service? if yes, this takes care of the requirement where I need to
make sure that I will only proceed further when previous service in
STOPPED status. But how will I kill this service if it gets stuck in
STOPPING status? I mean in my case some services do get stuck in
STOPPING status and we have to manually killl it right away. How can I
check if the service got stuck in STOPPING status and how should I
kill it?

Also, the above loop make sure that we proceed only when previous
service has completely stopped. Correct me, but I thought even the
following two commands also does the same job. I mean the following sc
command (using wait) parameter, makes sure that next service is only
started/stopped, when the current service has been completely started/
stopped. Is it not the case? I mean what does wait parameter mean
here?

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

TIA
 
P

Pegasus \(MVP\)

*** See below.












*** Probably, but if this is what you want then you should
*** drop the "start" command altogether:
*** sc \\Server_Name stop service_name1


*** In such cases you need a loop, e.g. like so:
***
*** sc \\Server_Name stop service_name1
*** :Loop1
*** ping localhost -n 10 > nul
*** sc \\Server_Name query service_name1 | find /i "stopped || goto Loop1
***
*** You should also add a loop counter in order to deal
*** with cases where the service can't be stopped.





- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks. But the requirement says that I have to stop certain services
(stop next service only when previous service has completely stopped)
and then start them in reverse order (again, the next service should
be started only when previous services has started completely).

So based on your inputs I was able to modify the code as below

REM ** stopping first service
sc \\server_name stop service_name1

REM ** looping to make sure service got completely stopped **

:Loop1
ping localhost -n 10 > nul
sc \\server_name query service_name | find /i "stopped" || goto Loop1

Does the loop breaks when it finds "stopped" status for a particular
service? if yes, this takes care of the requirement where I need to
make sure that I will only proceed further when previous service in
STOPPED status.
*** The || connector will cause the subsequent command
*** ("goto Loop1") to be executed if and only if sc.exe does
*** not output the word "stopped". What do YOU think will
*** happen when sc.exe does output the word "stopped"?

But how will I kill this service if it gets stuck in
STOPPING status? I mean in my case some services do get stuck in
STOPPING status and we have to manually killl it right away. How can I
check if the service got stuck in STOPPING status and how should I
kill it?

*** Best for you to become familiar with some basic loop
*** commands. Try this little batch file, then adapt it to your
*** current problem:
*** @echo off
*** set count=0
*** :Label1
*** set /a count=%count% + 1
*** if %count% LSS 10 goto Label1
*** echo Count is now %count%
***
*** I could, of course, give you a turn-key solution. On the other
*** hand I think that it will be to your benefit if you really
*** understand what your batch files do.

Also, the above loop make sure that we proceed only when previous
service has completely stopped. Correct me, but I thought even the
following two commands also does the same job. I mean the following sc
command (using wait) parameter, makes sure that next service is only
started/stopped, when the current service has been completely started/
stopped. Is it not the case? I mean what does wait parameter mean
here?

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2
*** The "/wait" parameter relates to the start command. It causes
*** the start command to wait until sc.exe returns control. This
*** often happens a long time before the service is stopped.

TIA
 
T

test

Thanks. But the requirement says that I have to stop certain services
(stop next service only when previous service has completely stopped)
and then start them in reverse order (again, the next service should
be started only when previous services has started completely).

So based on your inputs I was able to modify the code as below

REM ** stopping first service
sc \\server_name stop service_name1

REM ** looping to make sure service got completely stopped **

:Loop1
ping localhost -n 10 > nul
sc \\server_name query service_name | find /i "stopped" || goto Loop1

Does the loop breaks when it finds "stopped" status for a particular
service? if yes, this takes care of the requirement where I need to
make sure that I will only proceed further when previous service in
STOPPED status.
*** The || connector will cause the subsequent command
*** ("goto Loop1") to be executed if and only if sc.exe does
*** not output the word "stopped". What do YOU think will
*** happen when sc.exe does output the word "stopped"?

But how will I kill this service if it gets stuck in
STOPPING status? I mean in my case some services do get stuck in
STOPPING status and we have to manually killl it right away. How can I
check if the service got stuck in STOPPING status and how should I
kill it?

*** Best for you to become familiar with some basic loop
*** commands. Try this little batch file, then adapt it to your
*** current problem:
*** @echo off
*** set count=0
*** :Label1
*** set /a count=%count% + 1
*** if %count% LSS 10 goto Label1
*** echo Count is now %count%
***
*** I could, of course, give you a turn-key solution. On the other
*** hand I think that it will be to your benefit if you really
*** understand what your batch files do.

Also, the above loop make sure that we proceed only when previous
service has completely stopped. Correct me, but I thought even the
following two commands also does the same job. I mean the following sc
command (using wait) parameter, makes sure that next service is only
started/stopped, when the current service has been completely started/
stopped. Is it not the case? I mean what does wait parameter mean
here?

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2
*** The "/wait" parameter relates to the start command. It causes
*** the start command to wait until sc.exe returns control. This
*** often happens a long time before the service is stopped.

TIA- Hide quoted text -

- Show quoted text -

Thanks again Pegasus. Well as far as the loop is concerned, the
control will break out of the loop when it encounters "stopped". One
question, I just tried below command

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

Both services went in "starting" status at the same time. I also
checked logs of both services on servername and it seems both have
same start time. My question, is why the two services went in
"starting" status at the same time? I thought the "wait" parameter
will halt processing of next service until the current command
terminates? Also, logs shows the same start time for both services. Is
this expected?

TIA
 
P

Pegasus \(MVP\)

Thanks. But the requirement says that I have to stop certain services
(stop next service only when previous service has completely stopped)
and then start them in reverse order (again, the next service should
be started only when previous services has started completely).

So based on your inputs I was able to modify the code as below

REM ** stopping first service
sc \\server_name stop service_name1

REM ** looping to make sure service got completely stopped **

:Loop1
ping localhost -n 10 > nul
sc \\server_name query service_name | find /i "stopped" || goto Loop1

Does the loop breaks when it finds "stopped" status for a particular
service? if yes, this takes care of the requirement where I need to
make sure that I will only proceed further when previous service in
STOPPED status.
*** The || connector will cause the subsequent command
*** ("goto Loop1") to be executed if and only if sc.exe does
*** not output the word "stopped". What do YOU think will
*** happen when sc.exe does output the word "stopped"?

But how will I kill this service if it gets stuck in
STOPPING status? I mean in my case some services do get stuck in
STOPPING status and we have to manually killl it right away. How can I
check if the service got stuck in STOPPING status and how should I
kill it?

*** Best for you to become familiar with some basic loop
*** commands. Try this little batch file, then adapt it to your
*** current problem:
*** @echo off
*** set count=0
*** :Label1
*** set /a count=%count% + 1
*** if %count% LSS 10 goto Label1
*** echo Count is now %count%
***
*** I could, of course, give you a turn-key solution. On the other
*** hand I think that it will be to your benefit if you really
*** understand what your batch files do.

Also, the above loop make sure that we proceed only when previous
service has completely stopped. Correct me, but I thought even the
following two commands also does the same job. I mean the following sc
command (using wait) parameter, makes sure that next service is only
started/stopped, when the current service has been completely started/
stopped. Is it not the case? I mean what does wait parameter mean
here?

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2
*** The "/wait" parameter relates to the start command. It causes
*** the start command to wait until sc.exe returns control. This
*** often happens a long time before the service is stopped.

TIA- Hide quoted text -

- Show quoted text -

Thanks again Pegasus. Well as far as the loop is concerned, the
control will break out of the loop when it encounters "stopped". One
question, I just tried below command

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

Both services went in "starting" status at the same time. I also
checked logs of both services on servername and it seems both have
same start time. My question, is why the two services went in
"starting" status at the same time? I thought the "wait" parameter
will halt processing of next service until the current command
terminates? Also, logs shows the same start time for both services. Is
this expected?

================

Yes, it is fully expected. I mentioned before that you should drop the
"start" command. If you don't then you get exactly what you got here.
 
T

test

Thanks again Pegasus. Well as far as the loop is concerned, the
control will break out of the loop when it encounters "stopped". One
question, I just tried below command

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

Both services went in "starting" status at the same time. I also
checked logs of both services on servername and it seems both have
same start time. My question, is why the two services went in
"starting" status at the same time? I thought the "wait" parameter
will halt processing of next service until the current command
terminates? Also, logs shows the same start time for both services. Is
this expected?

================

Yes, it is fully expected. I mentioned before that you should drop the
"start" command. If you don't then you get exactly what you got here.- Hide quoted text -

- Show quoted text -

Thanks for the reply Pegasus. So I am able to come up with below code

REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (
set servicename=%%i

sc \\server_name start !servicename!

:Loop1
ping localhost -n 5 > nul
sc query !servicename! | find /i "running" || goto Loop1

echo !servicename! started successfully.

)

One question, is there still a way that this can be done using wait
option. Wherever I am reading about wait, it says it halts any further
processing until the current command is terminated. So isnt it same as
what we are doing in the above code? Sorry but I still had this
doubt.

Also at the end, I would like to send out a notification mail to
respective recipients. I cannot use any third party tool. I tried
mailto command but it opens up a new mail in outlook with respective
subject but does not send it. Why cant it send it automatically to
respective recipients? Can you, again, help me here. I do not want to
attach any files. Just a simple mail to 2-3 recipients with a subject
something like "serivces started successfully".

TIA
 
P

Pegasus \(MVP\)

Thanks again Pegasus. Well as far as the loop is concerned, the
control will break out of the loop when it encounters "stopped". One
question, I just tried below command

start /wait sc \\server_name stop service_name1
start /wait sc \\server_name stop service_name2

Both services went in "starting" status at the same time. I also
checked logs of both services on servername and it seems both have
same start time. My question, is why the two services went in
"starting" status at the same time? I thought the "wait" parameter
will halt processing of next service until the current command
terminates? Also, logs shows the same start time for both services. Is
this expected?

================

Yes, it is fully expected. I mentioned before that you should drop the
"start" command. If you don't then you get exactly what you got here.-
Hide quoted text -

- Show quoted text -

Thanks for the reply Pegasus. So I am able to come up with below code

REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (
set servicename=%%i

sc \\server_name start !servicename!

:Loop1
ping localhost -n 5 > nul
sc query !servicename! | find /i "running" || goto Loop1

echo !servicename! started successfully.

)

One question, is there still a way that this can be done using wait
option. Wherever I am reading about wait, it says it halts any further
processing until the current command is terminated. So isnt it same as
what we are doing in the above code? Sorry but I still had this
doubt.

Also at the end, I would like to send out a notification mail to
respective recipients. I cannot use any third party tool. I tried
mailto command but it opens up a new mail in outlook with respective
subject but does not send it. Why cant it send it automatically to
respective recipients? Can you, again, help me here. I do not want to
attach any files. Just a simple mail to 2-3 recipients with a subject
something like "serivces started successfully".

TIA

======================
I would simplify the batch file a little, e.g. like so:
@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (
sc \\server_name start %%i

:Loop1
ping localhost -n 5 > nul
sc query %%i | find /i "running" || goto Loop1

echo %%i started successfully.
)

As you see, there is no need for the "EnableDelayedExpansion" statement.

If you tell your son: "No TV watching until you've done your homework" then
you either believe him when he says that he's finished or else you check it
yourself. It's the same with sc.exe: If it terminates without finishing its
job then the "Start" command can do nothing at all about it, regardless of
the /wait parameter. It's YOUR responsibility to check the status of the
service to be stopped.

You can use this batch file to send an EMail message:
@echo off
set [email protected]
set [email protected]
set server=mail.company.com
set subject=Test Mail
set body=The quick brown fox

set schema=http://schemas.microsoft.com/cdo/configuration/
set scr=c:\TempVBS.VBS
echo> %scr% Set objEmail = CreateObject("CDO.Message")
echo>>%scr% With objEmail
echo>>%scr% .From = "%sender%"
echo>>%scr% .To = "%receiver%"
echo>>%scr% .Subject = "%subject%"
echo>>%scr% .Textbody = "%body%"
echo>>%scr% With .Configuration.Fields
echo>>%scr% .Item ("%schema%" ^& "sendusing") = 2
echo>>%scr% .Item ("%schema%" ^& "smtpserver") = "%server%"
echo>>%scr% .Item ("%schema%" ^& "smtpserverport") = 25
echo>>%scr% .Item ("%schema%" ^& "smtpauthenticate") = cdoBasic
echo>>%scr% .Item ("%schema%" ^& "sendusername") = "%sender%"
echo>>%scr% End With
echo>>%scr% .Configuration.Fields.Update
echo>>%scr% .Send
echo>>%scr% End With
cscript //nologo %scr%
del %scr%
 
T

test

Thanks for the reply Pegasus. So I am able to come up with below code

REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (
set servicename=%%i

sc \\server_name start !servicename!

:Loop1
ping localhost -n 5 > nul
sc query !servicename! | find /i "running" || goto Loop1

echo !servicename! started successfully.

)

One question, is there still a way that this can be done using wait
option. Wherever I am reading about wait, it says it halts any further
processing until the current command is terminated. So isnt it same as
what we are doing in the above code? Sorry but I still had this
doubt.

Also at the end, I would like to send out a notification mail to
respective recipients. I cannot use any third party tool. I tried
mailto command but it opens up a new mail in outlook with respective
subject but does not send it. Why cant it send it automatically to
respective recipients? Can you, again, help me here. I do not want to
attach any files. Just a simple  mail to 2-3 recipients with a subject
something like "serivces started successfully".

TIA

======================
I would simplify thebatchfile a little, e.g. like so:
@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (
  sc \\server_name start %%i

:Loop1
  ping localhost -n 5 > nul
  sc query %%i | find /i "running" || goto Loop1

  echo %%i started successfully.
)

As you see, there is no need for the "EnableDelayedExpansion" statement.

If you tell your son: "No TV watching until you've done your homework" then
you either believe him when he says that he's finished or else you check it
yourself. It's the same with sc.exe: If it terminates without finishing its
job then the "Start" command can do nothing at all about it, regardless of
the /wait parameter. It's YOUR responsibility to check the status of the
service to be stopped.

You can use thisbatchfile to send an EMail message:
@echo off
set [email protected]
set [email protected]
set server=mail.company.com
set subject=Test Mail
set body=The quick brown fox

set schema=http://schemas.microsoft.com/cdo/configuration/
set scr=c:\TempVBS.VBS
echo> %scr% Set objEmail = CreateObject("CDO.Message")
echo>>%scr% With objEmail
echo>>%scr%  .From = "%sender%"
echo>>%scr%  .To = "%receiver%"
echo>>%scr%  .Subject = "%subject%"
echo>>%scr%  .Textbody = "%body%"
echo>>%scr%  With .Configuration.Fields
echo>>%scr%   .Item ("%schema%" ^& "sendusing") = 2
echo>>%scr%   .Item ("%schema%" ^& "smtpserver") = "%server%"
echo>>%scr%   .Item ("%schema%" ^& "smtpserverport") = 25
echo>>%scr%   .Item ("%schema%" ^& "smtpauthenticate") = cdoBasic
echo>>%scr%   .Item ("%schema%" ^& "sendusername") = "%sender%"
echo>>%scr%  End With
echo>>%scr%  .Configuration.Fields.Update
echo>>%scr%  .Send
echo>>%scr% End With
cscript //nologo %scr%
del %scr%- Hide quoted text -

- Show quoted text -

Thanks again Pegasus. I am now using your suggestion and not including
"EnableDelayedExpansion" statement. Its working as expected. I tried
the above code but got following error:

C:\Documents and Settings\dir1\dir2\TempVBS.VBS(15, 2) CDO.Message.1:
The
"SendUsing" configuration value is invalid.

I tried searching on this error but could not find on what value I
should change "sendusing" value to. Can you please advise.

Also, as far as killing of services is concerned, I was able to come
up with following code. Just wanted to have your suggestion this.:

Each service is stopped and a delay is given after which the service's
status is checked. If service is still found in "stop_pending" (this
is status the job gets in after going to STOPPING status) status, I am
using taskkill command to kill the service. Does this look ok?

@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (

sc \\servername stop %%i

ping localhost -n 10 > nul

sc \\servername query %%i | find /i "stop_pending" || taskkill /s
computername /u userid /p password /im %%i
)

TIA
 
P

Pegasus \(MVP\)

*** See below.

Thanks again Pegasus. I am now using your suggestion and not including
"EnableDelayedExpansion" statement. Its working as expected. I tried
the above code but got following error:

C:\Documents and Settings\dir1\dir2\TempVBS.VBS(15, 2) CDO.Message.1:
The
"SendUsing" configuration value is invalid.

I tried searching on this error but could not find on what value I
should change "sendusing" value to. Can you please advise.
*** Sorry, I cannot comment unless you post the version of
*** the code you actually use.

Also, as far as killing of services is concerned, I was able to come
up with following code. Just wanted to have your suggestion this.:

Each service is stopped and a delay is given after which the service's
status is checked. If service is still found in "stop_pending" (this
is status the job gets in after going to STOPPING status) status, I am
using taskkill command to kill the service. Does this look ok?

@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (

sc \\servername stop %%i

ping localhost -n 10 > nul

sc \\servername query %%i | find /i "stop_pending" || taskkill /s
computername /u userid /p password /im %%i
)

*** You have the right idea but you're testing for the wrong
*** thing. Since you want to check if the service has stopped,
*** the code should look like so:
sc \\servername query %%i | find /i "stopped" || taskkill /s computername /u
userid /p password /im %%i

However, there is fundamental issue here. "%%i" is the name of the SERVICE
you wish to stop. Taskkill.exe will kill a TASK, not a SERVICE. What are you
trying to achieve with the taskkill command?
 
T

test

*** See below.


Thanks again Pegasus. I am now using your suggestion and not including
"EnableDelayedExpansion" statement. Its working as expected. I tried
the above code but got following error:

C:\Documents and Settings\dir1\dir2\TempVBS.VBS(15, 2) CDO.Message.1:
The
"SendUsing" configuration value is invalid.

I tried searching on this error but could not find on what value I
should change "sendusing" value to. Can you please advise.
*** Sorry, I cannot comment unless you post the version of
*** the code you actually use.

Also, as far as killing of services is concerned, I was able to come
up with following code. Just wanted to have your suggestion this.:

Each service is stopped and a delay is given after which the service's
status is checked. If service is still found in "stop_pending" (this
is status the job gets in after going to STOPPING status) status, I am
using taskkill command to kill the service. Does this look ok?

@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (

sc \\servername stop %%i

ping localhost -n 10 > nul

sc \\servername query %%i | find /i "stop_pending" || taskkill /s
computername /u userid /p password /im %%i
)

*** You have the right idea but you're testing for the wrong
*** thing. Since you want to check if the service has stopped,
*** the code should look like so:
sc \\servername query %%i | find /i "stopped" || taskkill /s computername/u
userid /p password /im %%i

However, there is fundamental issue here. "%%i" is the name of the SERVICE
you wish to stop. Taskkill.exe will kill a TASK, not a SERVICE. What are you
trying to achieve with the taskkill command?

Pegasus, below is the code I have used when trying to implement mail
functionality

@echo off

cd "C:\Documents and Settings\dir1\Desktop"

set [email protected]
set [email protected]

REM the server value was fetched using nslookup command

set server=name.abc.com
set subject=Test Mail
set body=The quick brown fox

set schema=http://schemas.microsoft.com/cdo/configuration/
REM set scr=TempVBS.VBS
set scr=TempVBS.VBS
echo> %scr% Set objEmail = CreateObject("CDO.Message")
echo>>%scr% With objEmail
echo>>%scr% .From = "%sender%"
echo>>%scr% .To = "%receiver%"
echo>>%scr% .Subject = "%subject%"
echo>>%scr% .Textbody = "%body%"
echo>>%scr% With .Configuration.Fields
echo>>%scr% .Item ("%schema%" ^& "sendusing") = 1
echo>>%scr% .Item ("%schema%" ^& "smtpserver") = "%server%"
echo>>%scr% .Item ("%schema%" ^& "smtpserverport") = 25
echo>>%scr% .Item ("%schema%" ^& "smtpauthenticate") = cdoBasic
echo>>%scr% .Item ("%schema%" ^& "sendusername") = "%sender%"
echo>>%scr% End With
echo>>%scr% .Configuration.Fields.Update
echo>>%scr% .Send
echo>>%scr% End With
cscript //nologo %scr%
del %scr%

pause

Pegasus, actually when I bring a service down using sc command, some
services on the
"My Computer->Manage->Services and applications"
shows as STOPPING. On querying their status using sc command, I found
that at this time their status was "stop_pending". So shouldnt I be
using "stop_pending" to kill these services instead of "stopped" as I
only want to kill those services that are stuck in STOPPING status and
not STOPPED?

Thanks for ponting. It seems taskkill is not the option I want to use
as I want to kill a SERVICE. I tried pskill but it seems it is not
installed. and I even have a restriction of not installing else. Any
other way?

TIA
 
P

Pegasus \(MVP\)

*** See below.


Thanks again Pegasus. I am now using your suggestion and not including
"EnableDelayedExpansion" statement. Its working as expected. I tried
the above code but got following error:

C:\Documents and Settings\dir1\dir2\TempVBS.VBS(15, 2) CDO.Message.1:
The
"SendUsing" configuration value is invalid.

I tried searching on this error but could not find on what value I
should change "sendusing" value to. Can you please advise.
*** Sorry, I cannot comment unless you post the version of
*** the code you actually use.

Also, as far as killing of services is concerned, I was able to come
up with following code. Just wanted to have your suggestion this.:

Each service is stopped and a delay is given after which the service's
status is checked. If service is still found in "stop_pending" (this
is status the job gets in after going to STOPPING status) status, I am
using taskkill command to kill the service. Does this look ok?

@echo off
REM service_names.txt contains all service names
REM This code is for starting services one after other

FOR /F %%i IN (service_names.txt) DO (

sc \\servername stop %%i

ping localhost -n 10 > nul

sc \\servername query %%i | find /i "stop_pending" || taskkill /s
computername /u userid /p password /im %%i
)

*** You have the right idea but you're testing for the wrong
*** thing. Since you want to check if the service has stopped,
*** the code should look like so:
sc \\servername query %%i | find /i "stopped" || taskkill /s computername
/u
userid /p password /im %%i

However, there is fundamental issue here. "%%i" is the name of the SERVICE
you wish to stop. Taskkill.exe will kill a TASK, not a SERVICE. What are
you
trying to achieve with the taskkill command?

Pegasus, below is the code I have used when trying to implement mail
functionality

@echo off

cd "C:\Documents and Settings\dir1\Desktop"

set [email protected]
set [email protected]

REM the server value was fetched using nslookup command

set server=name.abc.com
set subject=Test Mail
set body=The quick brown fox

set schema=http://schemas.microsoft.com/cdo/configuration/
REM set scr=TempVBS.VBS
set scr=TempVBS.VBS
echo> %scr% Set objEmail = CreateObject("CDO.Message")
echo>>%scr% With objEmail
echo>>%scr% .From = "%sender%"
echo>>%scr% .To = "%receiver%"
echo>>%scr% .Subject = "%subject%"
echo>>%scr% .Textbody = "%body%"
echo>>%scr% With .Configuration.Fields
echo>>%scr% .Item ("%schema%" ^& "sendusing") = 1
echo>>%scr% .Item ("%schema%" ^& "smtpserver") = "%server%"
echo>>%scr% .Item ("%schema%" ^& "smtpserverport") = 25
echo>>%scr% .Item ("%schema%" ^& "smtpauthenticate") = cdoBasic
echo>>%scr% .Item ("%schema%" ^& "sendusername") = "%sender%"
echo>>%scr% End With
echo>>%scr% .Configuration.Fields.Update
echo>>%scr% .Send
echo>>%scr% End With
cscript //nologo %scr%
del %scr%

pause

Pegasus, actually when I bring a service down using sc command, some
services on the
"My Computer->Manage->Services and applications"
shows as STOPPING. On querying their status using sc command, I found
that at this time their status was "stop_pending". So shouldnt I be
using "stop_pending" to kill these services instead of "stopped" as I
only want to kill those services that are stuck in STOPPING status and
not STOPPED?

Thanks for ponting. It seems taskkill is not the option I want to use
as I want to kill a SERVICE. I tried pskill but it seems it is not
installed. and I even have a restriction of not installing else. Any
other way?

TIA
=====================
I tested your script on my machine after adjusting the various names to my
own environment and after changing the line
echo>>%scr% .Item ("%schema%" ^& "sendusing") = 1
back to
echo>>%scr% .Item ("%schema%" ^& "sendusing") = 2
It worked perfectly.

You can look up your SMTP mail server with nslookup but there are some
further requirements to be considered:
- Does this server listen on port 25?
- Does it require user authentication?
A much easier way to get the server name and its prerequisites is to look
them up in your mail client (Outlook, Outlook Express). It's all there for
you to see!

If a service is stuck in the stop_pending state then there is nothing you
can do about it other than restarting the machine. AFAIK there is no command
to zap such a service. Ask the supplier of the service for further advice.
There might be a task that you need to shut down before you can kill the
service.
 
T

test

Pegasus, below is the code I have used when trying to implement mail
functionality

@echo off

cd "C:\Documents and Settings\dir1\Desktop"

set [email protected]
set [email protected]

REM the server value was fetched using nslookup command

set server=name.abc.com
set subject=Test Mail
set body=The quick brown fox

set schema=http://schemas.microsoft.com/cdo/configuration/
REM set scr=TempVBS.VBS
set scr=TempVBS.VBS
echo> %scr% Set objEmail = CreateObject("CDO.Message")
echo>>%scr% With objEmail
echo>>%scr%  .From = "%sender%"
echo>>%scr%  .To = "%receiver%"
echo>>%scr%  .Subject = "%subject%"
echo>>%scr%  .Textbody = "%body%"
echo>>%scr%  With .Configuration.Fields
echo>>%scr%   .Item ("%schema%" ^& "sendusing") = 1
echo>>%scr%   .Item ("%schema%" ^& "smtpserver") = "%server%"
echo>>%scr%   .Item ("%schema%" ^& "smtpserverport") = 25
echo>>%scr%   .Item ("%schema%" ^& "smtpauthenticate") = cdoBasic
echo>>%scr%   .Item ("%schema%" ^& "sendusername") = "%sender%"
echo>>%scr%  End With
echo>>%scr%  .Configuration.Fields.Update
echo>>%scr%  .Send
echo>>%scr% End With
cscript //nologo %scr%
del %scr%

pause

Pegasus, actually when I bring a service down using sc command, some
services on the
"My Computer->Manage->Services and applications"
shows as STOPPING. On querying their status using sc command, I found
that at this time their status was "stop_pending". So shouldnt I be
using "stop_pending" to kill these services instead of "stopped" as I
only want to kill those services that are stuck in STOPPING status and
not STOPPED?

Thanks for ponting. It seems taskkill is not the option I want to use
as I want to kill a SERVICE. I tried pskill but it seems it is not
installed. and I even have a restriction of not installing else. Any
other way?

TIA
=====================
I tested your script on my machine after adjusting the various names to my
own environment and after changing the line
echo>>%scr%   .Item ("%schema%" ^& "sendusing") = 1
back to
echo>>%scr%   .Item ("%schema%" ^& "sendusing") = 2
It worked perfectly.

You can look up your SMTP mail server with nslookup but there are some
further requirements to be considered:
- Does this server listen on port 25?
- Does it require user authentication?
A much easier way to get the server name and its prerequisites is to look
them up in your mail client (Outlook, Outlook Express). It's all there for
you to see!

If a service is stuck in the stop_pending state then there is nothing you
can do about it other than restarting the machine. AFAIK there is no command
to zap such a service. Ask the supplier of the service for further advice..
There might be a task that you need to shut down before you can kill the
service.- Hide quoted text -

- Show quoted text -

Thanks Pegasus. I will check and revery back accordingly. Thanks again
for all the help.
 
P

Pegasus \(MVP\)

Thanks Pegasus. I will check and revery back accordingly. Thanks again
for all the help.

===========

You're welcome.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top