?
Solved

Automate login, check windows service, logout.

Posted on 2010-08-16
18
Medium Priority
?
478 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
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
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
 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous article  (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_Server_2008/A_4466-A-beginners-guide-to-installing-SCCM2007-on-Windows-2008-R2-Server.html)detailed one possible method to get SCCM 2007 installed an…
Have you considered what group policies are backwards and forwards compatible? Windows Active Directory servers and clients use group policy templates to deploy sets of policies within your domain. But, there is a catch to deploying policies. The…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview

850 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