• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Automate login, check windows service, logout.

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
shellguy
Asked:
shellguy
  • 9
  • 8
1 Solution
 
B HCommented:
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
 
Dhiraj MuthaLevel DCommented:
This command will work - sc \\machinename stop/start <servicename>
0
 
shellguyAuthor Commented:
bryon44035v3:
Can you elaborate on what you mean by create a scheduled task?

thanks..
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
B HCommented:
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
 
B HCommented:
"now action required on your part after this."
should read "no action required on your part after this"
0
 
shellguyAuthor Commented:
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
 
shellguyAuthor Commented:
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
 
B HCommented:
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
 
shellguyAuthor Commented:
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
 
B HCommented:
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
 
shellguyAuthor Commented:
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
 
B HCommented:
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
 
shellguyAuthor Commented:
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
 
shellguyAuthor Commented:
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
 
B HCommented:
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
 
B HCommented:
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
 
shellguyAuthor Commented:
The solution provided worked for me. Thanks.
0
 
B HCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now