Solved

Automate login, check windows service, logout.

Posted on 2010-08-16
18
464 Views
Last Modified: 2012-05-10
Here are few scenarios i am trying to automate:
Scenario 1:
1) Login with an id. For most of the servers, i use a0... id, for a few servers, i use s0... id.
2) Check for different windows services are running or not, if not start them.
3) Log out.

Scenarios 2:
1) Login with an id. For most of the servers, i use a0... id, for a few servers, i use s0... id.
2) Log out.

Server: Windows 2003. Manually , i use RDP to connect to to different servers.

Let me know the best & simple way to achieve this.

thanks..
0
Comment
Question by:shellguy
  • 9
  • 8
18 Comments
 
LVL 24

Expert Comment

by:B H
ID: 33451698
you could simply create a scheduled task to run when not logged in, under whatever account you want, and have it run a batch file:

net start someservice

if it's already started, fine, the task ends.
if it's not started, it starts it, and the task ends
0
 
LVL 14

Expert Comment

by:Dhiraj Mutha
ID: 33451708
This command will work - sc \\machinename stop/start <servicename>
0
 

Author Comment

by:shellguy
ID: 33451753
bryon44035v3:
Can you elaborate on what you mean by create a scheduled task?

thanks..
0
 
LVL 24

Expert Comment

by:B H
ID: 33451821
sure-

first, get the service names that you want to make sure are started.  you can do this in start > run > services.msc
double click the service, look at the name, for example SomeService

make a batch file:
start > run > notepad
net start SomeService

then do file > save as > c:\start-the-services.bat
before you save it on this screen, select "all files" or it will get named as a .txt file
save it

obviouly you can name it whatever you want, or put it wherever you want.

then schedule the task:

start > programs > accessories > system tools > scheduled tasks

file > new task
name it, pick the batch file you made, uncheck "run only when logged on", type in a username, set the password, change the advanced options to run however often you want, hit ok a few times.

then, it runs... logged in or not, on its own, now action required on your part after this.
0
 
LVL 24

Expert Comment

by:B H
ID: 33451822
"now action required on your part after this."
should read "no action required on your part after this"
0
 

Author Comment

by:shellguy
ID: 33452071
bryon44035v3:
I forgot to mention: I want the script to run on my local machine that will remotely login to various other machines.

thanks..
0
 

Author Comment

by:shellguy
ID: 33452081
bryon44035v3:
I forgot to mention: I want the script to run on my local machine that will remotely login to various other machines, check different windows services and then logout.

thanks..
0
 
LVL 24

Expert Comment

by:B H
ID: 33465285
you can create the service as described above, on whatever machine you want - just be sure the username/password you choose for the task has the appropriate permissions on the destination machine

for the contents of the batch file, use the command shown by pspglb:
sc \\machinename stop/start <servicename>

example:

sc \\server2 start SomeService
0
 

Author Comment

by:shellguy
ID: 33472098
Couple more questions:
I created a batch file:
sc \\servername query servicename

1) Now, i want to check if the service is running, if not start it. If it is running, write the results to a file.
2) I am getting an error, when i tried to start a service:

[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.

Any idea as to why i am getting this error.

thanks.
0
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
LVL 24

Expert Comment

by:B H
ID: 33472145
1. you can change the command to this:

sc \\servername query servicename >> c:\the-log.txt
sc \\servername start servicename >> c:\the-log.txt
(the double >> means append to the file)

it seems that whaetver you typed for "servicename" is not a valid servicename.  on the server that has the service, double click the service and verify the servicename.  service names don't have spaces, should be the one near the top.
0
 

Author Comment

by:shellguy
ID: 33472298
I checked the servicename. I double clicked the servicename  on the server. The service name is "Changepoint Mail" which does has a space.

Also,
sc \\servername query servicename >> c:\the-log.txt
sc \\servername start servicename >> c:\the-log.txt

If the service is already running, does it start again (since first it queries and then it starts). I need to start only if it is not running.    
0
 
LVL 24

Expert Comment

by:B H
ID: 33472623
query only returns the state of the service, stop/started/paused
start will start the service if it's stopped, and will do nothing if it's already started

start will not "restart" the service, so there's no harm in sending it as a command.

there is no clear answer as to what the service name is on the internet, but to confirm, could you post a screenshot of that services properties?

if there is a space, you'll need to do this:
sc \\servername start "changepoint mail"

but, it's very odd to ahve a service with a space, so i want to confirm
0
 

Author Comment

by:shellguy
ID: 33472713
The quotes around the service name has worked. I wrote a small program like this:

@echo off
set log=checklog.txt
set Svcs="Changepoint Mail"
FOR %%a IN (%Svcs%) DO (SC \\PVMWCHP02 query %%a | FIND /i "RUNNING" >> "%log%"
IF ERRORLEVEL 1 SC start %%a)

Output: STATE: 4 RUNNING
If i need to display service name, how should i modify?
0
 

Author Comment

by:shellguy
ID: 33472839
I got how to display service name.
Now, if i need to check for ex: for 20 machines with different service names, shall i just repeat the code making the necessary changes or can the above code be modified to check for different machines and different service names.
0
 
LVL 24

Accepted Solution

by:
B H earned 125 total points
ID: 33487104
i figured you would set a scheduled task to run "every 20 minutes"

copy/paste the code in the same batch file, changing names where needed, would work fine.  if you want to set up a sort of array table to pull names of servers and services from, i think that's going to be a little more complicated than it's worth - copy/paste to replicate the code and edit does the job well enough
0
 
LVL 24

Expert Comment

by:B H
ID: 33487105
i figured you would set a scheduled task to run "every 20 minutes"

copy/paste the code in the same batch file, changing names where needed, would work fine.  if you want to set up a sort of array table to pull names of servers and services from, i think that's going to be a little more complicated than it's worth - copy/paste to replicate the code and edit does the job well enough
0
 

Author Closing Comment

by:shellguy
ID: 33498450
The solution provided worked for me. Thanks.
0
 
LVL 24

Expert Comment

by:B H
ID: 33499649
this question is the one that put me over 1,000,000 points by the way :)

POINT BREAKDOWN:

Points Assigned: 125
Grade: A (multiplier of 4)
-----------------------------
Points Earned Per Zone: 500

Grading comment: The solution provided worked for me. Thanks.

POINT TOTALS:

Overall: 1,000,282 (8,999,718 more points until Savant)
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Some time ago I faced the need to use a uniform folder structure that spanned across numerous sites of an enterprise to be used as a common repository for the Software packages of the Configuration Manager 2007 infrastructure. Because the procedu…
Know what services you can and cannot, should and should not combine on your server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now