Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Automate login, check windows service, logout.

Posted on 2010-08-16
18
466 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Case Summary: In this Article we introduce the new method to configure the default user profile using Automated profile copy with sysprep rather than the old ways such as the manual copy of a configured profile to default user profile Old meth…
Know what services you can and cannot, should and should not combine on your server.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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